thejoshwolfe
thejoshwolfe

Reputation: 5504

Is it possible to make real-time network games in JavaScript

Is it possible to make real-time network games using JavaScript? I've seen flash do it, but I'm interested in making a multiplayer browser-based game that doesn't depend on any plugins. I've read that it is impossible to keep Ajax connections open for streaming communication, and it isn't feasible to make several new Ajax connections per second to keep the client in sync with the server.

Upvotes: 7

Views: 4205

Answers (6)

michaelkoye
michaelkoye

Reputation: 56

Use WebRTC instead of WebSockets for access to peer-to-peer and UDP. See here: Does WebRTC use TCP or UDP? and WebRTC vs Websockets: If WebRTC can do Video, Audio, and Data, why do I need Websockets?

Upvotes: 4

thejoshwolfe
thejoshwolfe

Reputation: 5504

It looks like http://socket.io/ is good solution.

Upvotes: 4

oberhamsi
oberhamsi

Reputation: 1361

WebSockets are the solution for realtime (low latency) networking with JavaScript in the browser. There are fallbacks for providing the WebSocket API with Flash.

You can stick with JavaScript on the server and use something like http://RingoJs.org which has connectors for WebSockets. If you use those two you get at this:

// SERVER
websocket.addWebSocket(context, "/websocket", function(socket) {
  socket.onmessage = function(m) {
      // message m recieved from server
  };
  socket.send('my message to the client');
});

// CLIENT
var ws = new WebSocket("ws://localhost/websocket");
ws.onMessage(function(m) {
   // message m recieved from server
   // do something with it
   return;
});

ws.send('message to server');

Upvotes: 2

Eldar Djafarov
Eldar Djafarov

Reputation: 24685

As I know sockets without plugins is possible only in HTML5.
But you can use flash to accomplish this task. Since almost every browser supports flash now I think it is ok.
Also there are some hacks which allow do the same thing without bunch of ajax calls. Try to find Long Polling.
Hope it helps

Upvotes: 0

Peter C
Peter C

Reputation: 6307

It is. Look into a technology called Comet (like Ajax on steroids). Lift (a Scala web framework; twitter and others use it) has excellent Comet support build-in.

Upvotes: 0

Christian
Christian

Reputation: 28124

Yes, and it doesn't require any special libraries as certain people seem to imply.

Upvotes: -1

Related Questions