Adding WebSockets support to local web server - javascript

I'm developing a multiplayer Javascript/canvas game that uses WebSockets to communicate player positions and states etc. I'm comfortable coding this in Javascript, but it's the server-side websockets stuff that I'm having trouble with.
I have no idea how I should implement WebSocket support past the client side Javascript. Do I add a module to Apache? Do I do it in PHP? Do I use something like node.js? As far as I know node.js has received a lot of praise about this sort of stuff, but I still have no idea what it really is other than a web server.

I would recommend using nodejs, along with socket.io, which is a cross-browser compatible web sockets style library. It makes it really simple to write code which communicates with the server (==nodejs) in real time. It uses web sockets in browsers which support them, or other technologies in browsers which don't.
Just a note regarding PHP since you mentioned it... It is not very suitable for this in my opinion, since, as you may know, a PHP script only lives the duration of the request. This behavior makes it difficult to implement real time communication, unless you roll your own server with PHP, skipping Apache or such entirely.
Another alternative could be using Python, and perhaps something like Twisted, although you may need to implement websockets or such yourself.

Related

HTTP or Websocket for a low-latency web game?

I am new to web development and I just want to know some performance comparison.
I am building a tetris battle game on the web. Most of the game logics are inside of a server, which is built in C++. For clients, I am planning to use cocos2d-js, which is basically javascript.
Players' behavior will affect opponents. First, users' inputs need to get to the server correctly, the server applies inputs to the logic, and it will send a new game state back to clients. Client only needs to represent sprites on the screen.
My original plan was to implement it with TCP socket since it may need low latency. However, I just realized that we don't use TCP socket on the web.
Is it feasible to use HTTP connection for this kind of application?
Should I try web socket?
What are the pros and cons?
You can certainly use HTTP for this but for real-time communication the best option is using websockets.
Raw Sockets (aka, TCP sockets) are not part of the W3C standard.
Even though some browsers have implemented them, I'd recommend avoiding using them unless you don't care about locking your application with that specific browser.
In general, using Websockets will be your best option, they offer the same performance as raw sockets and are easier to use from JS.
Unfortunately, using a C++ server might be more complicated for this, since your server must implement the protocol, you can find some implementations on the internet like this one, for example.
Or, if you want to have a simpler integration with client/server you could try out Node.js to implement your server logic and use the Socket.io library to handle communication, which will also handle incompatible browsers gracefully. Note that Socket.io uses a different protocol, so I don't recommend trying to use it with your current C++ server if you want to avoid doing lots of extra work.
To sum things up
Raw (TCP) Sockets
Pros: easier to integrate with your current C++ server
Cons: browser support isn't guaranteed, not a (web) standard, will need more work for synchronization, communication, etc.
Websockets
Pros: performance on par with TCP sockets, easier to implement client-side
Cons: you might have to do more work on your server implementation
My personal recommendation would be avoiding TCP sockets if possible and stick to the standards.
Edit: Apparently, TCP sockets might end up being a W3C standard, the APIs are still a draft (and a pretty recent one), so I still recommend against using them (latest Chrome has an experimental implementation).
I think #Sosdoc has given the correct answer. The only thing i want to add that a libarary like SignalR can help you very much with the implementaton of websockets. You might want to check out the c++ signalr-qt project on github: https://github.com/p3root/signalr-qt

jQuery alternatives for multiplayer games?

First off, I am not asking for any code or anything like that.
all I need is some advise.
I'm creating a roulette game and everything in my roulette game is based on jQuery.
however, as we all know, jQuery is client side so I was thinking about using AJAX to send some details back to server and from the server to the users browser so I can make this roulette game work in "multiplayer" fashion... But the issue is that I don't think its possible to send the roulette's wheel animation to the server and back to users browser so I am a bit confused about this!
In my research I came across some information (old ones) about using node.js and jquery together! I don't know if this is possible or how easy it would be to use my jquery code in node.js as I have never used node.js before..
so the question that i have is:
based on the information i provided above and my requirements, what are my options?
can I use AJAX to achieve what i am trying to do and if so, a bit
information would be great.
if AJAX is out of question, is it possible to use my jquery code in
node.js to achieve what I am trying to do?
ANY ADVISE WOULD BE APPRECIATED.
P.S. I don't want to use HTML5 as 1st I don't know much about HTML5 and also, some devices, browsers do not support it.
Thanks
The best way is to use websockets to ensure real time communication. One of the best alternatives for implementing that could be using a server under node.js. Have a look to this video from Code School node.js tutorials: https://www.youtube.com/watch?v=mtDK4jf4RS0 where is ilustrated how to implement a real time chat. Your problem is based on the same.
There are three parts to a multiplayer game displayed in a browser:
client-side display,
server-side data management,
client-server communication.
If you're already set on your display technology (jQuery), then you're probably going to use $.ajax() for client-server communication. However, technologies used for server-side data management are completely up to you and they don't necessarily have any connection to the technologies used for display and communication (meaning the traditional communication initiated by client).
Basically, use any kind of server technology stack you like. Node.js might do just fine but there are many other viable alternatives. They just need to support communication with the client.
So, to be absolutely clear, your question doesn't really make sense. You might use jQuery in the client and Node.js on the server, but they will never really "work together". They will manage completely separate parts of your application and connect through protocols not specific to either of them.
As for the animation, the animation itself is solely a client-side problem. If you want to "synchronize the animation" across multiple clients, you can let the clients communicate with the server, but they only ever need to send/receive plain data. Then you adjust the client-side animation based on the received data.
As another poster points out, websockets are a better fit for this than regular client-initiated HTTP requests. Not because of "the animation", but because you want all the clients to receive the information about the start of betting at the same time.
I am also developing a MMO game using javascript only. Firstly, there are two different types of javascript here. Usual client side javascript (the one you already know) and the server side javascript (i.e. Node.js).
You will need to develop both client and server before connecting them with jQuery's Ajax. So you need to study Node.js before designing overall architecture of your game.
I read many Node.js tutorials and watched many youtube tutorial videos but I was still confused, before I really sat down and read a good textbook that explained from basics, one like below. Try to get hold of it from your local library.
Express web application development learn how to develop web applications with the Express framework from scratch by Hage Yaapa
Express is the popular framework that runs on Node.js now. So it's worth getting familiar with Express Framework. Once you understand how express app works (not so difficult), you will see how you can frame your game structure like I did :)
In order for many clients to share same animation, there must a server that synchronizes the game state. Ajax can only link between server-client communication in my understanding. There is no mechanism that connects between clients. The clients ask server what number was the result of roulette roll and show the corresponding animations.
Yes, you can use NodeJS and jQuery together.
NodeJS is server-side, meaning that you set up a server (a lot of people use the Express module, which is easy to use), which serves content to clients asking for it. Your server-side code can be in any language supporting sockets, not just NodeJS. But NodeJS is easy to use if you know JS already.
jQuery is client-side, meaning that it's executed by the user's browser, and may or may not have a server-side component (if it doesn't need it), or it might have one where it sends requests to the server-side code. When it requests a page from the server, it can be static content (like when you request index.html) or dynamic via an AJAX request. Client-side browser code must be HTML/CSS/JS (you can't make Firefox or Chrome interpret C, for example).

Understanding Node.js use cases

Trying to understand using node.js for a web applications.
Are there basically 2 major uses cases, i.e.:
The entire system is written in node, so you have functions for login, logout, password recover, and whatever else the web app does. All of this is written in javascript?
You use node.js only for sending the client updates, to have a real-time effect on the app. But the rest of the application is written in e.g. rails or django
Please tell me if I understand this correctly:
In terms of other technologies used with node.js, you tend to see people using node.js as the backend server, socket.io on the client side to establish a cross-browser long running ajax call library, and then you might use backbone.js for your client mvc pattern.
Is this right?
Basically speaking, it is just a tool to run javascript code server side. What you do with it is up to you. Many are using it as a complementary system since it's relatively new, but it's perfectly possible to run an standalone app with node.js.
It's said to be particularly good at handling concurrent connections, which is why it is often recommended to handle real-time jobs within an app, but there is no "obligation" so to speak to use it for this specific use case, it's just one thing you can do.
As with everything, the best way to understand it is to use it, so don't be afraid to play around with it.
Use case for Node js as we are using in our Application
Skype like voice & video chat on chrome browser using node js

Should I re-write my game server, write a forwarder, or use a new plugin?

I have a game server written in python and twisted, and an old game client also in python. I have written a new game client in Javascript, which will connect with websockets. Now I face a design decision, should I,
Re-write the game server in node.js, using miksago's websocket server and pure javascript
create a forwarder with node.js and miksago's websocket server, which will create a connection to the old python server with traditional sockets for each websocket connection it recieves.
Use gleicon's websocket server for twisted+python (this actually looks like a pretty good idea and I'm thinking I like it, but I'll post this question anyways.)
EDIT: links and clarity
There's a lot of info missing here.
Are you currently (un)happy with Python or Twisted or you game server implementation?
How large an effort do you estimate reimplementing it in JavaScript will be?
Are you already familiar with Node.js?
What advantages do you see in a JavaScript server implementation?
Are those advantages worth the effort to you?
Option 1 definitely looks scary, but it might not be for you. It's important you don't land in a situation where you derail your own project, because you've embarked on this huge effort that you may at some point lose the motivation for to finish.
Option 2 and 3 look easier to accomplish. I figure the main differences are having to learn Node.js and an extra daemon process to manage for option 2.
You can always choose to do 2 or 3, and move up to 1 if you're unsatisfied.
I assume you are doing this because you also want a browser based game client to be able to connect although it's not clear from your question.
You might look at wsproxy which is included with noVNC (HTML5 VNC client). It is a generic WebSockets to TCP socket proxy. wsproxy is used with noVNC to connect to VNC servers that don't have WebSockets support. There are three separate implementations of wsproxy in C, python and node.js.
Disclaimer: I made noVNC (and wsproxy).
If you're crazy and really know JavaScript a re-write should be a very good exercise in Node.js.
Although it depends on how much code you got, having both the client and the server written in JavaScript reduces context switching and gives you re-usability of the code if you're doing clients with lots of interpolation.
I'd say go for the rewrite if you want to learn Node.js and you already know how to do asynchronous programming.
Also, I did 2 game server with Node.js, so if you got further questions down the road I've got plenty of experience:
https://github.com/BonsaiDen/NodeGame-Orbit
https://github.com/BonsaiDen/NodeGame-Shooter
You may also want to check out BiSON to save on bandwidth with the WebSockets, I've written that specifically with HTML5 Games in mind:
https://github.com/BonsaiDen/BiSON.js
Yeah, node.js is really suitable for game server development.
The node.js network IO ability is much better than python.
But rewriting is a big price to pay, and you still can not take the advantage of scalability if there is only one node.js process.
There is a already a game server framework that support multiple process and more scalable, check this out:
https://github.com/NetEase/pomelo
And also, there is a full open source demo:
https://github.com/NetEase/lordofpomelo

Implementing COMET clientside

I have read up about what COMET streaming is, and all the various hacks required to get it working across the major browsers. The problems encountered seem to be two fold: 1. Server being able to support many persistent connections 2. Implementing the JS functionality
I have an application where I need to perform COMET streaming. I have already tackled the first problem above - I have a custom server running which delivers updates as it arrives in chunks. I want to now be able to connect using JS to this URL, so I can receive the updates.
Is there a simple COMET library I can use for this? I saw the Orbited project. Can I use the client side JS of orbited to perform this? What kind of modifications do I need to perform in the backend for this?
If you can get your server code to conform to the Bayeux specification then you can use any of the existing cometd javascript implementations, current available in DOJO and jQuery. Unfortunately, the spec is quite complex, especially if you want to do both long-polling and callback-polling. You may be better of just running an existing cometd server like Jetty and then writing a client library for your server code to push data to it.

Categories

Resources