原生nodejs使用websocket代码分享
安装:
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> 相关推荐
firejq 2020-06-14
woniyu 2020-06-02
取个好名字真难 2020-06-01
woniyu 2020-05-20
darylove 2020-05-14
shufen0 2020-04-18
shufen0 2020-04-14
guozewei0 2020-03-06
取个好名字真难 2020-02-13
sunnyJam 2020-02-16
katyusha 2020-01-28
xxg0 2020-01-09
guozewei0 2019-12-27
darylove 2019-12-27
wenf00 2019-11-08
竹叶青 2019-06-12
wangfuxu 2019-09-06
左手倒影右手年华 2018-05-21
83276636 2015-08-04