我们通过使用如下库创建websocket服务
go get golang.org/x/net/websocket
websocket服务端的代码如下:
package main;import ( "golang.org/x/net/websocket" "net/http" "log" "fmt")//使用golang.org/x/net/websocket//如果无法下载,请到下面的地址去下载//http://www.golangtc.com/download/package//http://gopm.io/type Msg struct { From string; To string; Data string;}//处理简单的字符串func test(conn *websocket.Conn) { var err error; for { data := ""; //接收消息 err = websocket.Message.Receive(conn,&data); if err != nil { break; } fmt.Println("clIEnt send: " + data); msg := "hello " + data; //发送消息 err = websocket.Message.Send(conn,msg); if err != nil { break; } }}//处理JsON数据func test2(conn *websocket.Conn) { var err error; for { var data Msg; //接收消息 err = websocket.JsON.Receive(conn,&data); if err != nil { break; } fmt.Println(data.From,data.To,data.Data); msg := Msg{ From: data.From,To: data.To,Data: "hello " + data.Data,}; //发送消息 err = websocket.JsON.Send(conn,msg); if err != nil { break; } }}func main() { //http.Handle("/",websocket.Handler(test)); http.Handle("/",websocket.Handler(test2)); err := http.ListenAndServe(":8080",nil); if err != nil { log.Fatal(err); }}
HTML客户端代码如下:
<!DOCTYPE HTML><HTML lang="en"><head> <Meta charset="UTF-8"> <Title>websocket</Title></head><body> <form> <input type="text" ID="msg"> <input type="submit" ID="sendBtn" value="发送"> </form> <div ID="result"></div> <script type="text/JavaScript"> var url = "ws://127.0.0.1:8080"; var ws = new WebSocket(url); //ws打开时 ws.onopen = function() { console.log("connect ..."); }; //ws连接关闭时 ws.onclose = function() { console.log("close"); }; //监听服务器推送数据 ws.onmessage = function(ev) { var result = document.getElementByID("result"); result.INNERHTML = result.INNERHTML + "<p>" + ev.data + "</p>"; }; document.getElementByID("sendBtn").onclick = function() { var msg = document.getElementByID("msg").value; if(msg.length == 0) { alert("不能为空"); } //发送数据 ws.send(msg); return false; }; </script></body></HTML>
@H_419_19@
@H_419_19@
Json格式:
<!DOCTYPE HTML><HTML lang="en"><head> <Meta charset="UTF-8"> <Title>websocket</Title></head><body> <form> <input type="text" ID="msg"> <input type="submit" ID="sendBtn" value="发送"> </form> <div ID="result"></div> <script type="text/JavaScript"> var url = "ws://127.0.0.1:8080"; var ws = new WebSocket(url); //ws打开时 ws.onopen = function() { console.log("connect ..."); }; //ws连接关闭时 ws.onclose = function() { console.log("close"); }; //监听服务器推送数据 ws.onmessage = function(ev) { var result = document.getElementByID("result"); var data = eval("(" + ev.data + ")"); result.INNERHTML = result.INNERHTML + "<p>" + data.From + "发送" + data.To + ":" + data.Data + "</p>"; }; document.getElementByID("sendBtn").onclick = function() { var msg = document.getElementByID("msg").value; if(msg.length == 0) { alert("不能为空"); } //发送数据 //注意这里JOSN的拼接,key和value要用双引号,否则go无法解析 ws.send('{"From":"小张","To":"小王","Data":"' + msg + '"}'); return false; }; </script></body></HTML>
@H_419_19@
@H_419_19@
总结以上是内存溢出为你收集整理的go语言websocket使用与客户端html5调用全部内容,希望文章能够帮你解决go语言websocket使用与客户端html5调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)