1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- </head>
- <body>
- <h2>学生B</h2>
- <div>
- <script src="jquery-1.8.2.js"></script>
- <script type="text/javascript">
- $(function () {
- var ws;
- //win服务中设置
- var url = "ws://127.0.0.1:8200";
- $("#btnConnection").click(function () {
- if ("WebSocket" in window) {
- ws = new WebSocket(url);
- }
- else if ("MozWebSocket" in window) {
- ws = new MozWebSocket(url);
- }
- else
- alert("浏览器版本过低,请升级您的浏览器。\r\n浏览器要求:IE10+/Chrome14+/FireFox7+/Opera11+");
- //注册各类回调
- ws.onopen = function () {
- $("#msg").append("连接服务器成功<br/>");
- ws.send("S:学生B已链接");
- }
- ws.onclose = function () {
- $("#msg").append("与服务器断开连接<br/>");
- }
- ws.onerror = function () {
- $("#msg").append("数据传输发生错误<br/>");
- }
- ws.onmessage = function (receiveMsg) {
- $("#msg").append(receiveMsg.data + "<br/>");
- }
- });
- $("#btnSend").click(function () {
- var text = $("#txtContent").val();
- ws.send("S:" + text);
- $("#msg").append("发送信息至服务器: " + "S:" + text + "<br/>");
- });
- });
- </script>
- <input id="btnConnection" type="button" value="连接" />
- <br />
- <textarea cols="100" rows="10" id="txtContent"></textarea>
- <br />
- <input id="btnSend" type="button" value="发送" />
- <hr />
- <span id="msg"></span>
- </div>
- </body>
- </html>
|