时间:2021-07-01 10:21:17 帮助过:33人阅读
服务端(nodejs):
var WebSocketServer = require('ws').Server, wss = new WebSocketServer({ port: 8080 }); wss.on('connection', function (ws) { console.log('client connected'); ws.on('message', function (message) { console.log(message); }); });
客户端:
<script> var ws = new WebSocket("ws://localhost:8080"); ws.onopen = function (e) { console.log('Connection to server opened'); sendMessage(); } function sendMessage() { ws.send('hello'); } </script>
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
微信小程序调用相机拍照
如何应用响应回写渲染页面
以上就是nodejs+websocket案例分析(附代码)的详细内容,更多请关注Gxl网其它相关文章!