Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Socket.io |类型错误:Socket.emit不是函数_Node.js_Sockets_Socket.io - Fatal编程技术网

Node.js Socket.io |类型错误:Socket.emit不是函数

Node.js Socket.io |类型错误:Socket.emit不是函数,node.js,sockets,socket.io,Node.js,Sockets,Socket.io,我见过很多问题,但没有一个能解决我的问题 以下是我的JS(app.JS)代码: var express=require('express'); var-app=express(); var serv=require('http')。服务器(应用程序); app.get('/',函数(req,res){ res.sendFile(uu dirname+'/client/index.html'); }); app.use('/client',express.static('/client'); se

我见过很多问题,但没有一个能解决我的问题

以下是我的JS(app.JS)代码:

var express=require('express');
var-app=express();
var serv=require('http')。服务器(应用程序);
app.get('/',函数(req,res){
res.sendFile(uu dirname+'/client/index.html');
});
app.use('/client',express.static('/client');
serv.listen(2000年);
log('服务器已启动');
var socket_list={};
var io=require('socket.io')(serv,{});
io.sockets.on('connection',函数(socket){
log(“套接字连接”);
socket.id=Math.random();
插座x=0;
y=0;
socket\u list=socket.id=socket;
});
setInterval(函数(){
用于(插座列表中的var i){
var socket=插座列表[i];
socket.x++;
socket.y++;
socket.emit('newPosition'{
x:socket.x,
y:插座
});
}
}, 1000/25);
以下是我的HTML(index.HTML)代码:


多人游戏| HTML5
var ctx=document.getElementById('ctx').getContext('2d');
ctx.font='24px Calibri';
var socket=io();
socket.on('newPosition',函数(数据){
ctx.clearRect(0,0500);
ctx.fillText('P',data.x,data.y);
})
我将遵循本教程:

它已经3年了,但在视频中4:47之前一切都很顺利

使用上面完全相同的代码^^^我得到:


您正在用
套接字对象替换套接字列表对象。也可以使用
io.on

io.sockets.on('connection', function(socket) {

    console.log('Socket Connection');

    socket.id = Math.random();
    socket.x = 0;
    socket.y = 0;
    socket_list = socket.id = socket;

});
你需要把它改成

io.on('connection', function(socket) {//use io.on

    console.log('Socket Connection');

    socket.id = Math.random();
    socket.x = 0;
    socket.y = 0;
    socket_list[socket.id] = socket;//fix this error

});
io.emit
更改为
socket.emit

socket.emit('newPosition', {
    x: socket.x,
    y: socket.y
});

(facepalm)原始代码是socket.emit,我将其更改为io.emit,因为我之前收到的stackoverflow问题中的一个答案告诉我将其更改为io。emit我现在已经修复了我的问题!即使在使用“socket.emit”时,仍然会发出错误。是否更改了socket_list=socket.id=socket;?也可以使用连接时的
io.on连接
,而不是
io.sockets连接
。编辑应答器更改套接字\u list=socket.id=socket;到
socket\u列表[socket.id]=socket
,这是最重要的错误。
socket.emit('newPosition', {
    x: socket.x,
    y: socket.y
});