| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ChatClient</title> |
| 5 | |
| 6 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 7 | |
| 8 | <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.css" /> |
| 9 | <link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/default/easyui.css" /> |
| 10 | <link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/icon.css" /> |
| 11 | <link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/demo/demo.css" /> |
| 12 | <script src="http://code.jquery.com/jquery-1.9.1.js"></script> |
| 13 | <script type="text/javascript" src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script> |
| 14 | <script type="text/javascript" src="qwebchannel.js"></script> |
| 15 | |
| 16 | <script> |
| 17 | 'use strict'; |
| 18 | var wsUri = "ws://localhost:12345"; |
| 19 | window.loggedin = false; |
| 20 | |
| 21 | window.onload = function() { |
| 22 | var socket = new WebSocket(wsUri); |
| 23 | |
| 24 | socket.onclose = function() { |
| 25 | console.error("web channel closed"); |
| 26 | }; |
| 27 | socket.onerror = function(error) { |
| 28 | console.error("web channel error: " + error); |
| 29 | }; |
| 30 | socket.onopen = function() { |
| 31 | window.channel = new QWebChannel(socket, function(channel) { |
| 32 | //connect to the changed signal of a property |
| 33 | channel.objects.chatserver.userListChanged.connect(function() { |
| 34 | $('#userlist').empty(); |
| 35 | //access the property |
| 36 | channel.objects.chatserver.userList.forEach(function(user) { |
| 37 | $('#userlist').append(user + '<br>'); |
| 38 | }); |
| 39 | }); |
| 40 | //connect to a signal |
| 41 | channel.objects.chatserver.newMessage.connect(function(time, user, message) { |
| 42 | $('#chat').append("[" + time + "] " + user + ": " + message + '<br>'); |
| 43 | }); |
| 44 | //connect to a signal |
| 45 | channel.objects.chatserver.keepAlive.connect(function(args) { |
| 46 | if (window.loggedin) { |
| 47 | //call a method |
| 48 | channel.objects.chatserver.keepAliveResponse($('#loginname').val()) |
| 49 | console.log("sent alive"); |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | }); |
| 54 | } |
| 55 | } |
| 56 | </script> |
| 57 | </head> |
| 58 | <body> |
| 59 | |
| 60 | <div id="loginDialog" class="easyui-dialog" title="Chat Login" data-options="iconCls:'icon-save'" style="width:400px;height:200px;padding:10px"> |
| 61 | <form id="loginForm" method="post"> |
| 62 | <table cellpadding="5"> |
| 63 | <tr><td>Name:</td><td><input class="easyui-validatebox" type="text" id="loginname" name="name" data-options="required:true"></input></td></tr> |
| 64 | </table> |
| 65 | </form> |
| 66 | <div style="text-align:center;padding:5px"> |
| 67 | <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">Login</a> |
| 68 | </div> |
| 69 | <div style="text-align:center;padding:5px" id="loginError"> |
| 70 | Username already in use. |
| 71 | </div> |
| 72 | <script> |
| 73 | $('#loginForm').submit(submitForm); |
| 74 | |
| 75 | function submitForm(event) { |
| 76 | console.log("DEBUG login: " + channel); |
| 77 | channel.objects.chatserver.login($('#loginname').val(), function(arg) { |
| 78 | console.log("DEBUG login response: " + arg); |
| 79 | if (arg === true) { |
| 80 | $('#loginError').hide(); |
| 81 | $('#loginDialog').dialog('close'); |
| 82 | window.loggedin = true; |
| 83 | } else { |
| 84 | $('#loginError').show(); |
| 85 | } |
| 86 | }); |
| 87 | console.log($('#loginname').val()); |
| 88 | if (event !== undefined) |
| 89 | event.preventDefault(); |
| 90 | return false; |
| 91 | } |
| 92 | </script> |
| 93 | </div> |
| 94 | |
| 95 | |
| 96 | <div class="easyui-layout" style="width:500px;height:300px;"> |
| 97 | <div data-options="region:'east',split:true" title="Users" id="userlist" style="width:100px;"> |
| 98 | |
| 99 | </div> |
| 100 | <div data-options="region:'south',split:true" style="height:50px;"> |
| 101 | <form id="messageForm"> |
| 102 | <input class="easyui-validatebox" type="text" id="message" name="name" style="width: 97%"></input> |
| 103 | </form> |
| 104 | </div> |
| 105 | <div data-options="region:'center'" id="chat"> |
| 106 | |
| 107 | </div> |
| 108 | <script> |
| 109 | $('#messageForm').submit(submitMessage); |
| 110 | |
| 111 | function submitMessage(event) { |
| 112 | channel.objects.chatserver.sendMessage($('#loginname').val(), $('#message').val()); |
| 113 | $('#message').val(''); |
| 114 | if (event !== undefined) |
| 115 | event.preventDefault(); |
| 116 | return false; |
| 117 | } |
| 118 | </script> |
| 119 | </div> |
| 120 | |
| 121 | |
| 122 | <script type="text/javascript"> |
| 123 | $(document).ready(function() { |
| 124 | $('#loginError').hide(); |
| 125 | }); |
| 126 | </script> |
| 127 | |
| 128 | </body> |
| 129 | </html> |