package main import ( "code.google.com/p/go.net/websocket" "fmt" "log" "net/http" "html/template" ) func Echo(ws *websocket.Conn){ var err error for{ fmt.Println("start") 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 cient: " + msg) if err = websocket.Message.Send(ws, msg); err != nil{ fmt.Println("Can't send") break } } } func chat(w http.ResponseWriter, r *http.Request){ r.ParseForm() fmt.Println(r.Form) t,_ := template.ParseFiles("websocket_demo.html") t.Execute(w, nil) } func main(){ http.Handle("/", websocket.Handler(Echo)) http.HandleFunc("/chat", chat) if err := http.ListenAndServe(":9999", nil); err != nil{ log.Fatal("ListentAndServe:", err) } } ========================================================================================== <html> <head> <body> <script type="text/javascript"> var sock = null; var wsuri = "ws://127.0.0.1:9999"; 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); } }; function send(){ console.log("send") var msg = document.getElementById('message').value; console.log(msg) sock.send(msg); }; </script> <h1> Websocket Echo Test</h1> <form> <p> Message: <input id = "message" type = "text" value ="hello dumx"> </p> </form> <button onclick="send();">Send Msg</button> </body> </head> </html>
详细解决方案
HTML5 websocket实验,后台老板为golang http包使用
热度:167 发布时间:2013-03-12 11:19:35.0
相关解决方案
- struts HTTP Status 500解决办法
- httpclient老是提示java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpCon
- 启动tomcat后,http://localhost:8080无法打开的有关问题
- 相干tomcat HTTP Status 500的疑问,麻烦大家进来看看
- Web Service 错误:服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/
- HTTP Status 404 - /jspdemo/Register.jsp,该怎么解决
- 'http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/,该如何处理
- org.apache.http.client.HttpClient与org.apache.commons.httpclient.HttpClient的差别
- HTTP 404有关问题
- myeclipse<%@ taglib prefix="c" uri="http://java.sun.com/jst1/core_rt"%>解决思路
- ,http 200状态码
- tomcat新手有关问题 HTTP Status 404 - /manager/
- java http 分段上载
- soap调用webservice,报错:Root element of a SOAP message must be: 'http://schemas.xmls,该怎么处理
- HTTP Status 404如何解决
- org.apache.tomcat.util.http.Parameters processParameters,该怎么解决
- 新手,HTTP Status 404 有关问题
- java http 断点上传,该如何处理
- JSP疑问<%@ taglib prefix="ht" uri="http://www.ht.com.cn/jsp/taglibs"%>该怎么处理
- weservice调用Server returned HTTP response code: 500 for URL解决方法
- HTTP Status 404 - /webproject2/servlet/login解决办法
- 报错:HTTP Status 405 - HTTP method GET is not supported by this URL解决思路
- http://newbbs.csdn.net/ 现时和老版的bbs用的还不是同一个库
- http://newbbs.csdn.net/ 现下和老版的bbs用的还不是同一个库
- Java.io.IOException: error 3 during http over WSP 这个是什么东东
- http://sourceforge.net这个网站是什么啊解决方案
- http 传送数据 每个包有大小限制码?解决办法
- Server returned HTTP response code: 403 for URL如何解决
- http://www.java-home.com重新下线,诚邀您的加入
- httpClient 错误 The server failed to respond with a valid HTTP response