WebSocketServer

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!