Merge pull request #7 from theromis/master
critical for writing from mutiple threads
This commit is contained in:
@@ -63,13 +63,20 @@ func (rc *RecConn) setIsConnected(state bool) {
|
|||||||
rc.isConnected = state
|
rc.isConnected = state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RecConn) getConn() *websocket.Conn {
|
||||||
|
rc.mu.RLock()
|
||||||
|
defer rc.mu.RUnlock()
|
||||||
|
|
||||||
|
return rc.Conn
|
||||||
|
}
|
||||||
|
|
||||||
// Close closes the underlying network connection without
|
// Close closes the underlying network connection without
|
||||||
// sending or waiting for a close frame.
|
// sending or waiting for a close frame.
|
||||||
func (rc *RecConn) Close() {
|
func (rc *RecConn) Close() {
|
||||||
rc.mu.RLock()
|
if rc.getConn() != nil {
|
||||||
defer rc.mu.RUnlock()
|
rc.mu.Lock()
|
||||||
if rc.Conn != nil {
|
|
||||||
rc.Conn.Close()
|
rc.Conn.Close()
|
||||||
|
rc.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
rc.setIsConnected(false)
|
rc.setIsConnected(false)
|
||||||
@@ -98,7 +105,9 @@ func (rc *RecConn) ReadMessage() (messageType int, message []byte, err error) {
|
|||||||
func (rc *RecConn) WriteMessage(messageType int, data []byte) error {
|
func (rc *RecConn) WriteMessage(messageType int, data []byte) error {
|
||||||
err := ErrNotConnected
|
err := ErrNotConnected
|
||||||
if rc.IsConnected() {
|
if rc.IsConnected() {
|
||||||
|
rc.mu.Lock()
|
||||||
err = rc.Conn.WriteMessage(messageType, data)
|
err = rc.Conn.WriteMessage(messageType, data)
|
||||||
|
rc.mu.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rc.closeAndReconnect()
|
rc.closeAndReconnect()
|
||||||
}
|
}
|
||||||
@@ -116,7 +125,9 @@ func (rc *RecConn) WriteMessage(messageType int, data []byte) error {
|
|||||||
func (rc *RecConn) WriteJSON(v interface{}) error {
|
func (rc *RecConn) WriteJSON(v interface{}) error {
|
||||||
err := ErrNotConnected
|
err := ErrNotConnected
|
||||||
if rc.IsConnected() {
|
if rc.IsConnected() {
|
||||||
|
rc.mu.Lock()
|
||||||
err = rc.Conn.WriteJSON(v)
|
err = rc.Conn.WriteJSON(v)
|
||||||
|
rc.mu.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rc.closeAndReconnect()
|
rc.closeAndReconnect()
|
||||||
}
|
}
|
||||||
@@ -210,7 +221,6 @@ func (rc *RecConn) setDefaultHandshakeTimeout() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (rc *RecConn) setDefaultDialer(handshakeTimeout time.Duration) {
|
func (rc *RecConn) setDefaultDialer(handshakeTimeout time.Duration) {
|
||||||
rc.mu.Lock()
|
rc.mu.Lock()
|
||||||
defer rc.mu.Unlock()
|
defer rc.mu.Unlock()
|
||||||
|
|||||||
Reference in New Issue
Block a user