本章主要讲解Go如何实现简易websocket通讯功能。
server.go,源码:
package main import ( "fmt" "golang.org/x/net/websocket" "html/template" "log" "net/http" ) const tpl = `<html> <head></head> <body> <script type="text/javascript"> var sock = null; var wsuri = "ws://127.0.0.1:1234"; window.onload = function() { console.log("onload"); sock = new WebSocket(wsuri); sock.onopen = function() { console.log("connected to " + wsuri); } sock.onclose = function(e) { console.log("connection closed (" + e.code + ")"); } sock.onmessage = function(e) { console.log("message received: " + e.data); document.getElementById('webyang_net').innerHTML = e.data; } }; function send() { var msg = 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> <button onclick="send();">Send Message</button> <h2 id='webyang_net'></h2> </body> </html>` func Echo(ws *websocket.Conn) { var err error for { var reply string if err = websocket.Message.Receive(ws, &reply); err != nil { fmt.Println("Can't receive") break } fmt.Println("Received back from client: " + reply) msg := "Received: " + reply fmt.Println("Sending to client: " + msg) if err = websocket.Message.Send(ws, msg); err != nil { fmt.Println("Can't send") break } } } func myHandler(w http.ResponseWriter, r *http.Request) { t := template.New("tpl") content, _ := t.Parse(tpl) content.Execute(w, nil) } func main() { http.Handle("/", websocket.Handler(Echo)) http.Handle("/send", http.HandlerFunc(myHandler)) if err := http.ListenAndServe("localhost:1234", nil); err != nil { log.Fatal("ListenAndServe:", err) } }
命令行下执行:go run server.go
浏览器访问:http://localhost:1234/send
ie浏览器不支持websocket,所以用google或firefox测试。
github:https://github.com/yangsir/beego_study
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/98500.html