问答详情
源自:4-3 封装Websocket

websocket读写消息不是线程安全问题

为什么说websocket读写消息不是线程安全,上节课讲的,因为一个http对应一个协程,这个方法绑定有一个协程的呢,为啥会不安全呢

提问者:qq_慕粉1313705 2019-07-25 23:31

个回答

  • thur
    2019-11-26 23:45:20

    在Godoc文档中有这一段

    Concurrency
    Connections support one concurrent reader and one concurrent writer.

    Applications are responsible for ensuring that no more than one goroutine calls the write methods (NextWriter, SetWriteDeadline, WriteMessage, WriteJSON, EnableWriteCompression, SetCompressionLevel) concurrently and that no more than one goroutine calls the read methods (NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler, SetPingHandler) concurrently.

    The Close and WriteControl methods can be called concurrently with all other
    methods.