123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <html>
- <title>测试</title>
- <style>
- *{
- margin:0;
- padding:0;
- }
- .meta{
- width:500px;
- height:800px;
- border-radius: 5%;
- border:3px solid #000;
- margin:10px auto;
- position:relative;
- }
- .login_info{
- width:100%;
- height:40px;
- border-bottom:3px solid #000;
- position:absolute;
- left:0;
- top:0;
- line-height:40px;
- text-align:center;
- display:none;
- }
- </style>
- <body>
- <div class="meta">
- <div class="login_info">
- </div>
- </div>
- <script type="text/javascript">
- if (typeof console == "undefined") {
- this.console = {
- log: function (msg) {}
- };
- }
- var ws, name, client_list={};
- function connect(){
- // 创建websocket
- ws = new WebSocket("ws://127.0.0.1:2346");
- // 当socket连接打开时,输入用户名
- ws.onopen = onopen;
- // 当有消息时根据消息类型显示不同信息
- ws.onmessage = onmessage;
- ws.onclose = function() {
- console.log("连接关闭,定时重连");
- connect();
- };
- ws.onerror = function() {
- console.log("出现错误");
- };
- }
- // 连接建立时发送登录信息
- function onopen()
- {
- if(!name)
- {
- name = prompt('输入你的名字:', '');
- if(!name || name=='null'){
- name = '游客';
- }
- }
- var login_data = '{"type":"login","client_name":"'+name.replace(/"/g, '\\"')+'","room_id":"房间1"}';
- console.log(login_data);
- ws.send(login_data);
- }
- // 服务端发来消息时
- function onmessage(e)
- {
- var data = eval("("+e.data+")");
- switch(data['type']){
- // 服务端ping客户端
- case 'ping':
- ws.send('{"type":"pong"}');
- break;;
- // 登录 更新用户列表
- case 'login':
- alert(data['data']);
- }
- }
- connect();
- </script>
- </body>
- </html>
|