这个代码是我从一个教学视频中扒过来的,
一模一样,操作步骤也一样,
实现的是客户端向服务端发送数据
但是他有效果,我却报错了
.
Uncaught ReferenceError: io is not defined
.
他客户端引用包的方式我也没看懂。
为什么是
http://localhost:8080/socket.io/socket.io.js
.
客户端
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<title></title>
<script scr="http://localhost:8080/socket.io/socket.io.js" charset="utf-8"></script>
<script>
let sock=io.connect('ws://localhost:8080/');
document.onclick=function(){
sock.emit('a',12,5);
};
</script>
</head>
<body>
</body>
</html>
.
服务端
const http=require('http');
const io=require('socket.io');
//1.创建http服务
let httpServer=http.createServer();
httpServer.listen(8080);
//2.创建webSocket服务
let wsServer=io.listen(httpServer);
wsServer.on('connection',function(sock){
sock.on('a',function(num1,num2){
console.log(`接收到了数据${num1}+${num2}`);
});
});
node部署