主页 > 知识库 > 网页基础 > HTML >

织梦站长工具箱

  • 织梦CMS标签生成器
  • DedeCMS模板手册
  • AG亚游正则表达式测试
  • HTML/JS互转
  • AG亚游内容分词工具
  • AG亚游JS/CSS压缩

HTML5 websocket实验

来源:中国IT实验室 作者:佚名 发表于:2013-03-12 13:35  点击:
[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); e
[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>

    有帮助
    (6)
    100%
    没帮助
    (0)
    0%
    关于我们 招贤纳士 联系我们 帮助中心 协议说明 网站地图 用户反馈 合作伙伴 捐助织梦