Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


JavaScript
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 )

JavaScript
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( );
Posted
Updated 24-Jul-17 18:09pm

1 solution

I fixed it...

I used


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


........... 


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900