[Go] to achieve websocket server

Direct subpackages can be achieved using the official websocket agreement,

golang.org/x/net/websocket
If you do not go to this package in this directory github download net library into the directory $ GOPATH / src / golang.org / x / net on the line
Server code:
package main

import (
    "time"
    "log"
    "net/http"
    "golang.org/x/net/websocket"
)
func main(){
    http.HandleFunc("/",index)
    http.Handle("/websocket",websocket.Handler(instantMessage))
    log.Println("start server")
    err:=http.ListenAndServe(":1234",nil)
    log.Println(err)
}
func index(res http.ResponseWriter,req *http.Request){
    res.Write([]byte("hello world"))
}
func instantMessage(w *websocket.Conn)  {
    var error error
    for {
        var reply string
        if  error= websocket.Message.Receive(w,&reply);error!=nil{
            log.Println("接受消息失败",error)
            break
        }
        log.Println("客户端:",reply)
        
        msg:="服务端:"+reply+time.Now().String()
        log.Println(msg)
        if error = websocket.Message.Send(w, msg); error != nil {
            log.Println("发送消息失败")
            break
        }
    }
}

Client code:

< HTML > 
< head > 
    < title > learn </ title > 
</ head > 
< body > 
< Script type = "text / JavaScript" > 
    var our sock =  null ;
     // var wsuri = "WSS: //127.0.0.1 : 8080 "; // local address can be changed oh 
     var wsuri =  " WS: // localhost: 1234 / WebSocket " ; // local address can be changed oh 


    the window.onload =  function () {
         //The client can see JS, very easy to establish a connection to the server through a sock WebSocket function, when the handshake succeeds, it will trigger onopen event WebScoket object, tell the client connection has been successfully established. A total of four bound client events. 
        the console.log ( " Start of the onload " ); 
        our sock =  new new a WebSocket (wsuri);
         // after establishing a connection triggers 
        sock.onopen =  function () { 
            the console.log ( " connection is established to trigger Connected "  + wsuri); 
        } 
        // close the connection when triggered 
        sock.onclose =  function (E) { 
            the console.log ( " to close the connection when triggered closed connection ( "  + e.code+  " ) " ); 
        } 
        // After receiving the trigger message 
        sock.onmessage =  function (E) { 
            the console.log ( " After receiving a trigger message Received Message: "  + e.data); 
            document.getElementById ( ' the receive ' ) .innerHTML = e.data; 
        } 
        // event of an error trigger 
        sock.onerror = function (E) { 
            the console.log ( " error occurred when trigger " + wsuri) 
        } 
    }; 
     //If the sock will herein be closed off being given ah 
    function Send () {
         var MSG = document.getElementById ( ' Message ' ) .Value; 
        sock.send (MSG); 
    }; 
</ Script > 
< h1 of > client < / h1 of > 
< form > 
    < P > transmitted: </ P > 
    < TextArea ID = "Message"  > I Tao Shihan </ TextArea > 
    < P > Receive: </p>
    < TextArea ID = "the receive"  > </ TextArea > 
</ form > 
< Button the onclick = "Send ();" > sends a message to the server </ Button > 
</ body > 
</ HTML >

 

 

 

Guess you like

Origin www.cnblogs.com/taoshihan/p/12037911.html