首页前端开发HTMLHTML5 websocket实验,后台为golang http包使用

HTML5 websocket实验,后台为golang http包使用

时间2024-01-25 10:08:12发布访客分类HTML浏览544
导读:收集整理的这篇文章主要介绍了html5教程-HTML5 websocket实验,后台为golang http包使用,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝...
收集整理的这篇文章主要介绍了html5教程-HTML5 websocket实验,后台为golang http包使用,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 [htML]  

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>  

 

[html]  

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教程,巧夺天工,精雕玉琢。小宝典献丑了!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

divHTMLImportpost-format-galleryTemplate

若转载请注明出处: HTML5 websocket实验,后台为golang http包使用
本文地址: https://pptw.com/jishu/586399.html
HTML 5 的自定义 data-* 属性和jquery的data()方法的使用 [html5游戏开发]数独游戏-完整算法-开源讲座

游客 回复需填写必要信息