安装:
npm install ws
服务端(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>
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“原生nodejs使用websocket代码分享”评论...