|
@@ -8,40 +8,62 @@
|
|
|
</head>
|
|
|
<body>
|
|
|
<div class="maincontent">
|
|
|
- <button id="shake">消息测试</button>
|
|
|
+ <input type="text" placeholder="输入内容" id="msg">
|
|
|
+ <button id="send_msg">发送信息</button>
|
|
|
+
|
|
|
+ <button id="shake">摇一摇</button>
|
|
|
+
|
|
|
+ <div class="msg_list"></div>
|
|
|
</div>
|
|
|
|
|
|
+
|
|
|
+<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL; ?>/mobile/shake/js/zepto.min.js"></script>
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
var token = "<?php echo $output['token']; ?>";
|
|
|
- var addr = "<?php echo $output['addr']; ?>";
|
|
|
+ var addr = "<?php echo $output['addr']; ?>";
|
|
|
|
|
|
var client = new WebSocket(addr);
|
|
|
- client.onopen = function()
|
|
|
- {
|
|
|
+ client.onopen = function () {
|
|
|
var msg = {
|
|
|
- act : 'access',
|
|
|
- op : 'login',
|
|
|
- token : token
|
|
|
+ act: 'access',
|
|
|
+ op: 'login',
|
|
|
+ token: token
|
|
|
};
|
|
|
client.send(JSON.stringify(msg));
|
|
|
};
|
|
|
|
|
|
- client.onerror = function()
|
|
|
- {
|
|
|
+ client.onerror = function () {
|
|
|
console.log('error');
|
|
|
};
|
|
|
- client.onclose = function(e)
|
|
|
- {
|
|
|
+ client.onclose = function (e) {
|
|
|
console.log('close');
|
|
|
};
|
|
|
- client.onmessage = function(msg)
|
|
|
- {
|
|
|
- console.log(msg);
|
|
|
+ client.onmessage = function (datas) {
|
|
|
+ var msg = datas.data;
|
|
|
+ var msgHTML = "<p>" + msg + "</p>";
|
|
|
+ $('.msg_list').append(msgHTML);
|
|
|
+ console.log(JSON.parse(datas.data));
|
|
|
};
|
|
|
|
|
|
|
|
|
- document.getElementById('shake').addEventListener('click',function(){
|
|
|
+ document.getElementById('send_msg').addEventListener('click', function () {
|
|
|
+ var inputVal = $('#msg').val();
|
|
|
+ if(!inputVal) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var msg = {
|
|
|
+ act: 'access',
|
|
|
+ op: 'login',
|
|
|
+ token: token,
|
|
|
+ content:inputVal
|
|
|
+ };
|
|
|
+ client.send(JSON.stringify(msg));
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ document.getElementById('shake').addEventListener('click', function () {
|
|
|
client.send('1');
|
|
|
});
|
|
|
|