TypeError:socket.emit不是函数
TypeError: socket.emit is not a function
我正在尝试编写基本的在线游戏,只是为了检查我目前的网络编程水平。这是我现在的一些代码:
var io = require('socket.io') (serv, {});
io.sockets.on('connection', function(socket) {
console.log('1 '+socket);
function fight (player,monster) {
console.log('2 '+socket);
monsterData = {};
monsterData.name = monster;
switch(monster){
case 'rat 1st level':
monsterData.hp = 50;
monsterData.attack = 1;
monsterData.money = Math.floor(Math.random()*3)+1;
break;
}
result = attack(player,monsterData);
console.log('3 '+socket);
if(result.winner === 'player'){
db.account.findOne({'username':player.username},function(err,player){
money = player.money + monsterData.money;
db.account.update({'username':player.username},{$set:{'money':money}});
socket.emit('addToChat',{type:'system',message:'You win battle against "'+monster+'"'})
});
}
}
console.log('4 '+socket);
socket.on('click',function(data){
db.account.find({'username':data.username,'socket':data.socket},function(err,res){
if(res.length > 0) {
position = 0;
//console.log('x - '+data.x+'; y- '+data.y);
/* RIGH BAR */
if(data.x>(1024-200) && data.x<1024 && data.y>80) {
// if(data.y>50&&data.y<80)
// position=1;
if(data.y>80&&data.y<110){
monster='rat 1st level';
}
if(data.y>110&&data.y<140){
monster='2ndMonster';
}
if(data.y>140&&data.y<170){
monster='3rdMonster';
}
if(data.y>170&&data.y<210){
monster='4thMonster';
}
db.account.findOne({'username':data.username},function(err,res){
db.account.update({'username':res.username},{$set:{'inFight':true,'monster':monster}});
io.sockets.emit('fightStart',{'username':res.username,'monster':monster});
result = fight(res,monster);
});
/* /RIGHTBAR */
}
//console.log(position);
}
});
});
socket.on('signIn',function(data){
isValidPassword(data,function(res){
if(res){
db.account.find({'username':data.username},function(err,player){
player = player[0];
socket.emit('signInResponse',{
success:true,
username:player.username,
password:player.password,
level:player.level,
money:player.money,
hp:player.hp,
maxhp:player.maxhp
});
});
} else {
socket.emit('signInResponse',{success:false,reason:'wrong login or password'});
}
});
});
}
所以,基本上,我可以在socket.on('signin')
中获取socket变量,但当我试图在函数fight或socket.on('click')
中获取它时,它会给我一个错误,即套接字没有定义,或者给我错误,因为套接字只是客户端的socket.id
,我不能用它来发送数据。
我需要什么
一些如何使可变套接字在战斗功能中可以访问,以具有向特定玩家发送数据的能力。
您应该直接使用io:
io.on('connection', function(socket){
socket.on('xxx', function(obj){
io.emit('xxx', {xxx: xxx})
})
})
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- TypeError:socket.emit不是函数
- 理解用于emit函数esp的Node.js代码
- Parsley.remote.js抛出错误- $.Emit不是一个函数
- node.js中的回调函数.TypeError: Cannot call method 'emit'的定
- Node.js-emit vs函数调用
- 如何在AngularJS Emit中传递函数作为参数?
- apply() 不适用于 Node.js eventemitter 的 emit() 函数
- 在规则集全局部分的emit中,函数声明后面没有分号