1

Background: I'm using the full Express, http.Server, and Socket.IO to make a game. When each person connects, they have a socket session and I use that to send information between server and client.

What I would like to do now is to add AI to my game, since there is a minimum number of players and AI would make the experience richer. However, I don't know how to pretend each AI player is on the other end of a socket. Is there a way that I could do something like 'var fakeSocket = new Socket(...)'?

nectarsac
  • 53
  • 8

2 Answers2

3

You can also open the client connection from your server to your server using "socket.io-client"

var clientIo = require('socket.io-client');
var socket = clientIo.connect(...);

Then use it like normal socket to add a new player to the game.

Tero Tolonen
  • 4,144
  • 4
  • 27
  • 32
  • Thanks for the help! I have just one question: what am I supposed to pass to the connect() call? I'm trying 'localhost:3000' for now, but it doesn't look like my AI is connecting. – nectarsac May 12 '15 at 22:04
  • I have used var socket = clientIo.connect("http://"+node.ip+":"+node.port); and then socket.on("connect", function() { ... – Tero Tolonen May 12 '15 at 22:11
  • I found out what I needed: clientIo.connect('http://localhost:3000') For future reference, what is node in your code? – nectarsac May 12 '15 at 22:13
  • Just internal variables, node.ip = and node.port = – Tero Tolonen May 12 '15 at 22:19
2

Your AI players can run on your server and just connect to the server (on the same box) with a webSocket connection. Your server doesn't even need to know if they are AI players or real players. As far as the server is concerned, they are webSocket connections that come from somewhere (it just so happens that some of them come from the same box rather than a distant browser). In this manner, the server doesn't need to treat the AI players any differently than the regular players (unless you choose to invoke different logic for each to favor one over the other).

So, there's no need to use a fake socket. You can more easily just use a real webSocket connection and then there's no socket pretending required.

See this other question/answer for how to create a socket.io connection from your server:

Is it possible to set up a socket.io client running (server-side) on a node.js server?

In summary, you use the socket.io-client module:

var io = require('socket.io-client');

And, then use the io variable like you would from the browser client.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That's what I was hoping for. In that case, how can I have some JS object create a real Socket connection? The way the examples do it, the client calls io() but I'm not sure how to set that up for an AI object. – nectarsac May 12 '15 at 21:13
  • @nectarsac - see what I added to the end of my answer for how you create a socket.io connection from your server. – jfriend00 May 12 '15 at 21:18
  • Thanks for the help! One last thing. I'm using connect('localhost:3000') but it's not securing the connection. What could be wrong with this? – nectarsac May 12 '15 at 22:02
  • @nectarsac - you'd have to show more details of the specific code you were using for me to know what was wrong. The socket.io client documentation shows this: `var socket = io('http://localhost');` or this: `var socket = io('http://localhost:3000');`. In other words, it should be an actual URL. – jfriend00 May 12 '15 at 22:14
  • The problem was that 'localhost:3000' wasn't accepted but 'http://localhost:3000' is allowed, so thanks for everything! – nectarsac May 13 '15 at 00:35
  • @nectarsac - so my help doesn't even deserve an upvote? – jfriend00 May 13 '15 at 00:38
  • sorry man, I'm new to stackoverflow and I guess I can't give upvotes until my reputation is 15 or something like that. I guess I also can't mark both of you guys as the correct answer either, since both answers are correct – nectarsac May 14 '15 at 01:32