• HTML5 websocket实验


    [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>

  • 相关阅读:
    Spring Boot 详细简介
    Linux 安装 MySQL 8 数据库(图文详细教程)
    有了这个日期工具类,让日期转化不再烦恼
    Linux常用实用命令
    Java分割中英文,并且中文不能分割一半?
    Spring MVC或Spring Boot配置默认访问页面不生效?
    js如何判断当前页面是否处于激活状态
    博客园 & 陌上花开HIMMR | 脱单倒计时!只能帮你到这了
    博客园 & 陌上花开HIMMR | 距2020年脱单,只剩34天!
    博客园 & 陌上花开HIMMR | 脱单倒计时!刚过完10.24的你,还想一个人过11.11吗?
  • 原文地址:https://www.cnblogs.com/wushihan/p/6007239.html
Copyright © 2020-2023  润新知