nodejs——js 实现webSocket 兼容移动端
//服务器端 //npm install --save ws const express = require('express'); const http = require('http'); const url = require('url'); const WebSocket = require('ws'); const app = express(); app.use(function (req, res) { res.send({ msg: "hello" }); }); const server = http.createServer(app); const wss = new WebSocket.Server({ server }); var stocks = { "con1": 95.0, "con2": 50.0, "con3": 300.0, "con4": 550.0, "con5": 35.0 } function randomInterval(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } var stockUpdater; var randomStockUpdater = function() { for (var symbol in stocks) { if(stocks.hasOwnProperty(symbol)) { var randomizedChange = randomInterval(-150, 150); var floatChange = randomizedChange / 100; stocks[symbol] += floatChange; } } var randomMSTime = randomInterval(500, 2500); stockUpdater = setTimeout(function() { randomStockUpdater(); }, randomMSTime); } randomStockUpdater(); wss.on('connection', function (ws) { var sendStockUpdates = function (ws) { if (ws.readyState == 1) { var stocksObj = {}; for (var symbol in clientStocks) { stocksObj[symbol] = stocks[symbol]; } if (stocksObj.length !== 0) { ws.send(JSON.stringify(stocksObj));//需要将对象转成字符串。WebSocket只支持文本和二进制数据 console.log("更新", JSON.stringify(stocksObj)); } } } var clientStockUpdater = setInterval(function () { sendStockUpdates(ws); }, 5000); ws.on('message', function (message) { var stockRequest = JSON.parse(message);//根据请求过来的数据来更新。 console.log("收到消息", stockRequest); clientStocks = stockRequest; sendStockUpdates(ws); }); }); server.listen(8080, function listening() { console.log('Listening on %d', server.address().port); });
//客户端 vue,写成普通html 直接用浏览器访问也可以 <template> <div> <button @click="check"> 启动 </button> <div id="con1"></div> <div id="con2"></div> <div id="con3"></div> <div id="con4"></div> <div id="con5"></div> </div> </template> <script> export default { data () { return { } }, methods:{ check(){ var isClose = false; var stocks = { "con1": 0, "con2": 0, "con3": 0, "con4": 0, "con5": 0 }; var WebSocketsExist = true; try { var ws = new WebSocket("ws://10.20.11.180:8080"); } catch (ex) { try { ws = new MozWebSocket("ws://10.20.11.180:8080"); } catch(ex) { WebSocketsExist = false; } } if (WebSocketsExist) { alert("您的浏览器支持WebSocket. 您可以尝试连接到聊天服务器!"); } else { alert("您的浏览器不支持WebSocket。请选择其他的浏览器再尝试连接服务器。"); } function updataUI() { ws.onopen = function (e) { console.log('Connection to server opened'); isClose = false; ws.send(JSON.stringify(stocks)); console.log("sened a mesg"); } //更新UI var changeStockEntry = function (symbol, originalValue, newValue) { document.getElementById(symbol).innerHTML = symbol+' : '+newValue.toFixed(2); } // 处理受到的消息 ws.onmessage = function (e) { var stocksData = JSON.parse(e.data); console.log(stocksData) console.log('-----------------------------------------'); for (var symbol in stocksData) { if (stocksData.hasOwnProperty(symbol)) { changeStockEntry(symbol, stocks[symbol], stocksData[symbol]); stocks[symbol] = stocksData[symbol]; } } }; } updataUI(); } }, } </script>
https://www.cnblogs.com/stoneniqiu/p/5402311.html