Overview
WebSocketServer is a simple, lightweight and high-performance
Webserver for HTML5-WebSocket applications. It was written in C++ using
non-blocking sockets to obtain a low overhead. The server is scriptable
using LUA which provides endless possibilities for all kinds of
applications (eg. game servers, chat servers or realtime-displays).
Using the WebSocketServer is very simple - you'll need to write
the server logic in LUA and the HTML5-frontend in JavaScript. A very
basic LUA-Script ("echo-server") would look like this:
-- Called when a message is recieved
function main (clientId, message)
-- echo back the message
_send(clientId, "You said: " .. message)
end
Building the HTML5 frontend is done using the window.WebSocket
object. The JavaScript code could look something like this:
function socketExample() {
var socket;
if (window.hasOwnProperty("WebSocket")) { // webkit-browser
socket = new WebSocket("ws://localhost:8085/");
}
else if (window.hasOwnProperty("MozWebSocket")) { // firefox
socket = new MozWebSocket("ws://localhost:8085/");
}
else { // browser doesnt support websocket
alert("Your browser doesn't support WebSocket.");
return;
}
socket.onopen = function() { // the socket is ready, send something
socket.send("Wow, HTML5-WebSocket is nice!");
};
socket.onmessage = function(msg) { // the server send something
alert("The server said: " + msg.data);
};
socket.onclose = function() { // the server closed the connection
alert("The server closed the connection.");
};
}
// run the example
socketExample();
That's pretty much all the code you'll have to write to get started
with the WebSocketServer! Check out the howto`s and the download section!
HowTo
Lua scripting
Write a lua-server script
As described on the homepage, writing a lua script for the server is very
simple. All you need to do is implement the following functions:
function main (clientId, message)
Description:
This function is called everytime a client sends a message to the server
Parameters:
int clientId is an integer and identifies a client. Every client has
it's own unique clientId. When a client disconnects, a new client can take that ID.
str message is the message sent by the client identified by clientId.
Example:
-- Called when a message is recieved
function main (clientId, message)
-- echo back the message
_send(clientId, "You said: " .. message)
end
function on_connect(clientId)
Description:
This function gets called everytime a new client connects.
Parameters:
int clientId (see main-function)
Example:
-- Gets called when a client connects
function on_connect(clientId)
_sendAll("ClientID " .. clientId .. " just joined the server")
end
function on_disconnect(clientId)
Description:
This function gets called everytime a client disconnects.
Parameters:
int clientId (see main-function)
Functions you can use from your lua-script
To send out responses to clients, you may use the following functions:
_send(clientId, message)
Description:
Send a message to a client, identify by clientId
Parameters:
int clientId (see main-function)
str message Message to send
_sendAll(message)
Description:
Send a message to all connected clients
Parameters:
str message Message to send
C++ Hooks
If you don't want to write your server application in lua, you'll have to grab
the source code of the server and implement your own hook. Your hook will have
to implement the following "interface", as given in wsHookInterface.h:
class wsHookInterface {
public:
virtual ~wsHookInterface() {};
virtual void hServerStartup(wsServerInterface *server) = 0;
virtual void hServerShutdown() = 0;
virtual void hClientConnect(int i) = 0;
virtual void hClientDisconnect(int i) = 0;
virtual void hClientMessage(int i, std::string message) = 0;
};
Then, in the main file, replace the line "wsLuaHook hook;" to your own hook. Don't
forget to include it's header-file!
Download
Please not that these are early releases and not ment to be used in production.
Only for experimental and testing purposes.
Download websocket0.2b.zipg (~69 KB)
Windows Binaries (in folder ./bin/win)
Contains the main .exe file and a sample lua
script. You may need to download and put the lua.dll into the main directory,
depending on your system configuration.
SourceCode (in folder ./src)
The C++ sourcecode of this websocket server. In order to compile it you
must link it against against the LUA libraries. I propose using the gcc
compiler. I tried to make the server work on both windows and linux systems,
but I haven't yet run any tests on linux.
Browser support
- Firefox: since version 6.0
- Chrome: since version 14
- Opera: since version 10.70
- Safari: WebSocket is supported, but uses an older protocol version which
doesn't work with the WebSocketServer
FAQ
Can I use this server in production?
No, please don't! It's still in an early development phase and there may
be critical bugs. The current releases are just experimental and ment for
testing purposes.
How can I help improving this server?
Just grab the source and improve it - if you like you can then send your
patches to [YOU NEED JAVASCRIPT TO SEE MY EMAIL].
Bugs
If you do finde a bug please report it to .