func TestConn_multiple(t *testing.T) { s := &mockServer{} var buffers []*bytes.Buffer for i := 0; i < 10; i++ { // TODO: use newMockConn() b := &bytes.Buffer{} conn := message.NewConn(0, b, b, s.Write) s.conns = append(s.conns, conn) buffers = append(buffers, b) } // One message sent over all ios s.Write(func(w io.Writer) error { return builder.SendPing(w) }) for i, conn := range s.conns { msgType := handler.ReadType(conn) if msgType != message.Types["ping"] { t.Error("Sent ping, but didn't received it over io", i) } } // Multiple messages sent over multiple conns chatCount := 10 chatMsg := "Hello World! What's up bro?" var wg sync.WaitGroup for i := 0; i < chatCount; i++ { wg.Add(1) go (func() { defer wg.Done() s.Write(func(w io.Writer) error { return builder.SendChatSend(w, chatMsg) }) })() } wg.Wait() for _, conn := range s.conns { for i := 0; i < chatCount; i++ { msgType := handler.ReadType(conn) if msgType != message.Types["chat_send"] { t.Error("Sent chat_send, but didn't received it at iteration", i) } receivedMsg := handler.ReadChatSend(conn) if receivedMsg != chatMsg { t.Error("Bad received chat message content at iteration", i) } } } }
// Read client data from channel func (c *Client) listen() { log.Println("New client:", c.id) // Send binary frames // See http://grokbase.com/t/gg/golang-nuts/1314ee50mh/go-nuts-sending-binary-websocket-frames#20130104bmvbvtymkitzju2nhnaytrrzs4 c.conn.PayloadType = 0x2 defer c.Close() reader := bufio.NewReader(c.conn) conn := message.NewConn(c.id, reader, c.conn, c.Server.Write) c.Server.handler.Listen(conn) }
// Creates new Client instance and starts listening func (s *Server) newClient(conn net.Conn) { r := bufio.NewReader(conn) w := bufio.NewWriter(conn) wc := struct { *bufio.Writer io.Closer }{w, conn} c := &Client{ Conn: message.NewConn(len(s.clients), r, wc, s.Write), Server: s, } s.clients = append(s.clients, c) c.Server.Joins <- c }
func newMockConn() *message.Conn { b := &bytes.Buffer{} return message.NewConn(0, b, b, nil) }