}
func (handler *hixiFrameHandler) WriteClose(_ int) (err os.Error) {
+ handler.conn.wio.Lock()
+ defer handler.conn.wio.Unlock()
closingFrame := []byte{'\xff', '\x00'}
handler.conn.buf.Write(closingFrame)
return handler.conn.buf.Flush()
}
func (handler *hybiFrameHandler) WriteClose(status int) (err os.Error) {
+ handler.conn.wio.Lock()
+ defer handler.conn.wio.Unlock()
w, err := handler.conn.frameWriterFactory.NewFrameWriter(CloseFrame)
if err != nil {
return err
}
func (handler *hybiFrameHandler) WritePong(msg []byte) (n int, err os.Error) {
+ handler.conn.wio.Lock()
+ defer handler.conn.wio.Unlock()
w, err := handler.conn.frameWriterFactory.NewFrameWriter(PongFrame)
if err != nil {
return 0, err
"json"
"net"
"os"
+ "sync"
"url"
)
buf *bufio.ReadWriter
rwc io.ReadWriteCloser
+ rio sync.Mutex
frameReaderFactory
frameReader
+ wio sync.Mutex
frameWriterFactory
frameHandler
// will read the rest of the frame data.
// it reads Text frame or Binary frame.
func (ws *Conn) Read(msg []byte) (n int, err os.Error) {
+ ws.rio.Lock()
+ defer ws.rio.Unlock()
again:
if ws.frameReader == nil {
frame, err := ws.frameReaderFactory.NewFrameReader()
// Write implements the io.Writer interface:
// it writes data as a frame to the WebSocket connection.
func (ws *Conn) Write(msg []byte) (n int, err os.Error) {
+ ws.wio.Lock()
+ defer ws.wio.Unlock()
w, err := ws.frameWriterFactory.NewFrameWriter(ws.PayloadType)
if err != nil {
return 0, err
if err != nil {
return err
}
+ ws.wio.Lock()
+ defer ws.wio.Unlock()
w, err := ws.frameWriterFactory.NewFrameWriter(payloadType)
_, err = w.Write(data)
w.Close()
// Receive receives single frame from ws, unmarshaled by cd.Unmarshal and stores in v.
func (cd Codec) Receive(ws *Conn, v interface{}) (err os.Error) {
+ ws.rio.Lock()
+ defer ws.rio.Unlock()
if ws.frameReader != nil {
_, err = io.Copy(ioutil.Discard, ws.frameReader)
if err != nil {