[DEV] WebSocket Server - Direct connection between webbrowser and gameserver


A WebSocket protocol implementation to create a direct connection between webbrowsers and gameservers.

WebSocket is a protocol designed to create a realtime connection between webbrowsers and webservers via javascript to exchange asynchronous information without reloading the page or requiring a client poll/page refresh.

This plugin provides an interface to create such an WebSocket server via SourceMod. Now you're able to do a live webchat seeing ingame events or even following the game in your browser like SourceTV or exchange whatever information you want.

This plugin doesn't open a server by itself and doesn't do much alone. It's a base for other plugins to implement fancy stuff without knowing the actual protocol.


This implementation follows the draft version 17 of the websocket protocol specifications, except:

  • No support for fragmented messages. (Would've been too much of a hassle, but could still be added, if really needed)

  • One websocket per plugin. Unlimited amount of connections though.

  • Multiple plugins opening a websocket on the same ip:port combo both share the WebSocket handle. The socket is only closed, if all plugins using it closed it. If one plugin closes the socket, the forwards are no longer fired, but the socket is still open until the last plugin closes it.

  • You can't use CloseHandle on websocket handles, since this is implemented in sourcepawn, which doesn't support adding "Handle" types. Use Websocket_Close instead.

  • You should always close your master websocket in OnPluginEnd().

  • This adapts to HTTP which is using TCP for connection management. You'll need to have the TCP port unblocked in your firewall to allow reading and writing.

Plugin Interface


PHP Code:

 * Creates a websocket server which listens on the supplied ip:port combination.
 * @param sHostName        The IP to bind to.
 * @param iPort            The port to listen on
 * @param inc            The incoming child connection callback
 * @param we            The error callback
 * @return                A WebsocketHandle or INVALID_WEBSOCKET_HANDLE on error.
na tive WebsocketHandle:Websocket_Open(const String:sHostName[], iPortWebsocketIncomingCB:incWebsocketErrorCB:weWebsocketCloseCB:clo);

 * Hooks child socket's events
 * @param childwebsocket    The child websocket to hook.
 * @param recv                Data receive callback
 * @param disc                The disconnect ca llback
 * @param we                The error callback
 * @return                    True if child socket was hooked, false otherwise
native bool:Websocket_HookChild(WebsocketHandle:childwebsocketWebsocketReceiveCB:recvWebsocketDisconnectCB:discWebsocketErrorCB:we);

 * Sends text or binary data through the websocket
  ;* @param childwebsocket    The child websocket to send to
 * @param type                The datatype SendType_Text or SendType_Binary
 * @param sPayLoad             The data to send
 * @param dataSize            If set, it's used as maxlength. Useful for binary data where  might be used before the end of the data.
 * @return                    True if child socket was hooked, false&nbs p;otherwise
native bool:Websocket_Send(WebsocketHandle:childwebsocketWebsocketSendType:type, const String:sPayload[], const dataSize=-1);

 * Gets a child websocket's readyState.
 * @param childwebsocket    The child websocket
 * @return                    The readyState
native WebsocketReadyState:Websocket_GetReadyState(WebsocketHa ndle:childwebsocket);

 * Unhooks a child socket's events: If there's no plugin listening anymore, the socket is closed.
 * @param childwebsocket    The child websocket
 * @noreturn
native Websocket_UnhookChild(WebsocketHandle:childwebsocket);

 * Closes a listening master socket, created with Websocket_Open.
 * Note: The socket will still be open, if there are more plugins using it.
 * Call this in OnPluginEnd()!
 * @pa ram websocket    The master websocket
 * @noreturn
native Websocket_Close(WebsocketHandle:websocket); 


Compiling dependencies

Thanks to

  • sfPlayer and his Socket extenstion

  • SirLamer - Base64 include

Server owners only need the websocket.smx in their plugins folder and the Socket extension installed. This plugin won't compile on the forum due to it's custom include requirements.

Attached Files

File Type: sp

Get Plugin or
Get Source (websocket.sp - 35.1 KB)

File Type: smx

websocket.smx (17.6 KB)

File Type: inc

websocket.inc (5.2 KB)

Orignal From: [DEV] WebSocket Server - Direct connection between webbrowser and gameserver

No comments

Not a single link is allowed to submit in comment :o