Client.emit在服务器端socket.io没有响应 [英] Client.emit doesnt response in server side, socket.io

查看:549
本文介绍了Client.emit在服务器端socket.io没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是node,express和socket.io的新手。我正在尝试使用这些技术构建一个游戏.. 

我有global.js,我用它作为服务器端。从客户端到侧面的每个操作或反之亦然。

现在我正在尝试构建一个计算机播放器。所以我在服务器端创建了bot.js文件。每当我创建具有不同客户端ID的新计算机播放器时,我在控制台中成功连接。

但是当我尝试发出一个任务('joinTable')时,我看不到任何反应。

如果有人帮我解决这个问题,我会很高兴。任何建议也没关系





我尝试过:



 < pre> bot.js服务器端文件 





  var  io = require('  socket.io-client'); 
function bot(client){
this .socket = {};
.createBot = function (){
this .socket = io.connect( http://192.45.47.44:3000 ,{ 强制新连接 true });
}
this .createBot();

this .socket.on(' connectionSuccess' function (args){
console .log (' 连接成功为',args.id)
client.emit(' joinTable',args);
});

返回 ;
}

function computerPlayer(){
return {
createBot: function (client){
var botPlayer = new bot(client);
return botPlayer;
}
}
}

module.exports = new computerPlayer();





 global.js服务器端文件(我也用于客户端的同一文件)



  var  io = require('  socket.io'); 
var computerPlayer = require(' ./ bot');
function Io(){
return {
init:< span class =code-keyword> function (server){
var objServ = io.listen(server);

objServ.sockets.on(' connection' function (client){

computerPlayer.createBot(client);

client.on(' joinTable' function (args){
console .log(' joinTable client id',args .id);
});

client.emit(' connectionSuccess' ,{
id:client.id
});

});
}
}
}
module.exports = new Io();

解决方案

我修好了... 

我用过





  var  soc =  this  .socket = io.connect(ip,{ 强制新连接 true }); 





 ........ ... 





 soc.emit('  joinTable',args);  //  而不是使用客户端我在bot.js中使用了soc.emit() 


I am new to node, express and socket.io. I am trying to build a game using these technology..

I have global.js which i am using as server side. every action from client side to side or vice versa works fine.

Now i am trying to build a computre player. So I have created bot.js file in server end. I am getting connection successfull in console whenever i create new computer player with different client id.

But when i try to emit a task ('joinTable') no response i can see.

I will be greatfull if someon help me to solve this. Any suggestion is also fine



What I have tried:

<pre>bot.js serverside file



var io = require( 'socket.io-client' );
    function bot( client ) {
    this.socket = {};
    this.createBot = function ( ) {
        this.socket = io.connect( "http://192.45.47.44:3000", { "force new connection": true } );
    }
    this.createBot( );

    this.socket.on( 'connectionSuccess', function ( args ) {
        console.log( 'connection successfull for ', args.id )
        client.emit( 'joinTable', args );
    } );

    return this;
    }

    function computerPlayer( ) {
    return {
        createBot: function ( client ) {
            var botPlayer = new bot( client );
            return botPlayer;
        }
    }
    }

    module.exports = new computerPlayer( );



global.js server side file ( same file I am using for client-side also )


var io = require( 'socket.io' );
    var computerPlayer = require( './bot' );
    function Io( ) {
    return {
        init: function ( server ) {
            var objServ = io.listen( server );

            objServ.sockets.on( 'connection', function ( client ) {

                computerPlayer.createBot( client );

                client.on( 'joinTable', function ( args ) {
                    console.log( 'joinTable client id', args.id );
                } );

                client.emit( 'connectionSuccess', {
                    id: client.id
                } );

            } );
        }
    }
    }
    module.exports = new Io( );

解决方案

I fixed it...

I used



var soc = this.socket = io.connect( ip, { "force new connection": true } );



........... 



soc.emit('joinTable', args); //instead of using client i used soc.emit() in bot.js


这篇关于Client.emit在服务器端socket.io没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆