golang-samples / websocket

samples of websocket package
445 stars 126 forks source link

websocket-chat logical problem #8

Open joyhope opened 8 years ago

joyhope commented 8 years ago

Simple run the demo, it works, but the flow has problem.

func (c *Client) listenRead() the api is never exited, always blocked on the c.doneCh on the second time

        err := websocket.JSON.Receive(c.ws, &msg)
        if err == io.EOF {
            c.doneCh <- true   <<==

After first c.doneCh <-true is send, the select will immediate run default flow. The Receive will get io.EOF again, but at that time, the listenWrite is exit because of c.donCh. The c.doneCh is not buffered, so it will be blocked for ever.

The simple fix is add time.Sleep(xxx) after the c.doneCh <-true. (Any way it is not a good fix.)