Node Client (2)

来源:互联网 发布:java代理模式的好处 编辑:程序博客网 时间:2024/05/21 22:47
<html><head> <script src="./js/jquery-1.12.3.min.js"></script>  <script>    var ws = new WebSocket("ws://localhost:13002");  ws.onopen = function(e) {     //连接时触发openx();    };     function sendMessage() {       // ws.send($('#message').val());   ws.send('hello');    //send data     }; function openx(){ $("#statx").val("open server"); };ws.onclose = function(evt) {  //通信断开时自动触发        $("#statx").val("close");     };    function exit() {           var r = ws.close();     //关闭连接    console.log("退出", r);  }  ws.onerror = function(evt) {        $("#statx").val("error");};      ws.onmessage = function (evt) {//后台返回消息的时候触发        $("#xxx").val(evt.data);      };    </script></head><body><form role="form" id="chat_form" onsubmit="sendMessage(); return false;">                <div class="form-group">                    <input class="form-control" type="text" name="message" id="message"                           placeholder="Type text to echo in here" value="" />                </div>                <button type="button" id="send" class="btn btn-primary"                        onclick="sendMessage();">                    Send!                </button>            </form><input  type="text" name="xx" id="xxx"                            value="sss" /><br><br><input  type="text" name="statx"   id="statx"                            value="sss" /></body></html>