func client(sess muxado.Session, id string, wg *sync.WaitGroup) { s, err := sess.OpenEx(muxado.StreamInfo(id)) if err != nil { log.Fatal(id, "open", err) } stream := &Stream{s} defer func() { stream.log("closing stream") stream.Close() if wg != nil { wg.Done() } }() stream.log("client loop") for i := 0; i < 10; i++ { stream.Write([]byte("hello there " + strconv.Itoa(i))) buffer := make([]byte, 1024) if n, err := stream.Read(buffer); err == nil { log.Println(id, "read", string(buffer[:n])) } else { log.Println(id, "read", err) break } } }
func handleSession(sess muxado.Session, id string) { defer sess.Close() for { log.Println(id, "accept") stream, err := sess.Accept() if err != nil { log.Println(id, "accept", err) return } else { go handleStream(&Stream{stream}, id) } } }