Download the WebSocket pack for go.
1. The official Google method requires Hg to synchronize the code. Because of the wall, you also need to set up an agent. More Trouble
2. http://gopm.io/through the website, this is Golang China to provide solutions Http://www.golangtc.com/download/package
The implementation of WebSocket is still relatively simple. Altogether there were 4 documents. Client.go hybi.go Server.go Websocket.go
The sample code is everywhere on the web and it's not posted.
There is an article of the WebSocket agreement that I have implemented once http://www.cnblogs.com/yjf512/archive/2013/02/18/2915171.html
It's better to understand the protocol. Of course this code is limited to learning.
Identify two things by Source:
1. WebSocket will automatically combine a full frame when it is received. That is, send and recv are one by one corresponding.
2. Why is it empty to send JSON data (later found not related to websocket 0.0)
1th. Is that I seldom use the web framework. It's ignorant. Later asked the Java colleague, now the framework has basically done the automatic disassembly package.
Golang's WebSocket supports two codec. Message and JSON form.
A Message is a stream of characters that is sent directly.
JSON is the parser that automatically calls the JSON inside.
Delivery in C + + is generally controlled precisely. JSON wastes a lot of redundant data. Although it is lighter than XML. But the benefits of extensibility and versatility are obvious.
This part of the code is in Websocket.go.
var JSON = Codec{jsonmarshal, Jsonunmarshal}
var Message = Codec{marshal, unmarshal}
Receive is handled as follows:
//Receive receives single frame from WS, unmarshaled by CD. Unmarshal and stores in V.Func (CD Codec) Receive (ws *conn, VInterface{}) (err error) {...//omittedagain:frame, err:=Ws.frameReaderFactory.NewFrameReader ()ifErr! =Nil {returnErr} frame, err=Ws.frameHandler.HandleFrame (frame)ifErr! =Nil {returnErr}ifframe = =Nil {Gotoagain} payloadtype:=frame. Payloadtype () data, err:=Ioutil. ReadAll (frame)ifErr! =Nil {returnErr}returnCD. Unmarshal (data, Payloadtype, v)}
The processing code for frame is in Hybi.go.
//Newframereader reads a frame header from the connection, and creates new reader for the frame.//See sections 5.2 Base Framing protocol for detail.// HTTP://TOOLS.IETF.ORG/HTML/DRAFT-IETF-HYBI-THEWEBSOCKETPROTOCOL-17#section -5.2func (buf hybiframereaderfactory) Newframereader () (frame Framereader, err error) {hybiframe:=New(Hybiframereader)//... Resolves the websocket protocol header. Hybiframe.reader=io. Limitreader (BUF. Reader, hybiFrame.header.Length) HybiFrame.header.data=bytes. Newbuffer (header) Hybiframe.length= Len (header) +int(hybiFrame.header.Length)return}
The data for the complete frame is accepted here according to Header.length.
The main handleframe is to judge the payloadtype of frame.
2nd. This is the pit of Golang's struct.
For lowercase members. The other packages are inaccessible. So JSON's marshal and unmarshal are empty. However, there is no error or abnormality. I have been struggling for a long day.
There are two solutions:
1. The first letter of the member variable is capitalized.
2. Implement JSON. Marshaler interface
For the second type, you can refer to this article. http://blog.csdn.net/varding/article/details/38560681
Golang's WebSocket source analysis