This is a creation in Article, where the information may have evolved or changed.
chatting for everyone is not strange, but how to do a chat function of their own
first introduce what is WebSocket, help everyone better understand WebSocket
Only one-way communication can be achieved through HTTP in the browser, comet can simulate two-way communication to some extent, but the efficiency is low and the server needs better support. Sockets and Xmlsocket in Flash enable true two-way communication, and with Flex Ajax Bridge, you can use both of these features in JavaScript. It is foreseeable that if WebSocket is implemented in a browser, it will replace the above two technologies and be widely used. In the face of this situation, HTML5 defines the WebSocket protocol, which can save server resources and bandwidth better and achieve real-time communication.
See why use WebSocket to write chat function, this can save a lot of trouble!
Because I wrote it with go. First you have to understand the go language, but you don't have to know how to use the go language, or I can't help it.
It's all right! Go to the chase.
The first is the customer's code
<script type="text/javascript" src="Jquery-1.11.0.js" > </script> <script type="Text/javascript"> varSock =NULL;varWsuri ="ws://localhost:9999"; Window.onload = function() {Console.log ("onload"); Sock =NewWebSocket (Wsuri); Sock.onopen = function() {Console.log ("Connected to"+ Wsuri); } Sock.onclose = function(e) {Console.log ("Connection closed ("+ E.code +")"); } sock.onmessage = function(e) {$('. ps '). Append (e.data+"</br>"); Console.log ("message Received:"+ E.data); } }; function send() { varmsg = document.getElementById (' message '). Value; Sock.send (msg); };</script> <H1>WebSocket Echo Test</H1> <form> <p>Message:<input id="message" type="text" value="Hello, world! " > </P> </form> <div Style=' width:100px,height:200px,border:1px solid red '> <p class="PS"></P> </div> <button onclick="Send ();" >Send Message</button>
Service-side code
PackageMainImport("Golang.org/x/net/websocket" "FMT" "Log" "Net/http")funcEcho (WS *websocket. Conn) {varErr error for{varReplystring ifErr = WebSocket. Message.receive (WS, &reply); Err! =Nil{FMT. Println ("Can ' t receive") Break} FMT. Println ("Received Back from client:"+ Reply) Msg: = Reply FMT. Println ("Sending to client:"+ msg)ifErr = WebSocket. Message.send (WS, MSG); Err! =Nil{FMT. Println ("Can ' t send") Break} }}funcMain () {http. Handle ("/", WebSocket. Handler (Echo))ifERR: = http. Listenandserve (": 9999",Nil); Err! =Nil{log. Fatal ("Listenandserve:", err)}}
* Note that you need to download go websocket library, because I am writing with Windows system, so the library has been modified slightly
This is the download address for the WebSocket library under Windows:/HTTP download.csdn.net/detail/qq_35730500/9736074
This is the download address for the WebSocket library under Linux: Http://download.csdn.net/detail/qq_ 35730500/9736079*
Because the official library needs to be downloaded at Code.google's address, upload it
and that's it!