123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <!DOCTYPE html>
- <html>
- <body>
- <h2>考场</h2>
- <div>
- <script src="jquery-1.8.2.js"></script>
- <script type="text/javascript">
- $(function () {
- var ws;
- //ip是远程服务端的ip,端口是远程服务端的端口
- var url = "ws://127.0.0.1:28800";
- $("#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("K:考场已链接");
- }
- 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("K:" + text);
- ws.send("readcardno");
- $("#msg").append("发送信息至服务器: " + "K:" + 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>
|