Raised This Month: $12 Target: $400
 3% 

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


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Plugin ID:
2900
Plugin Version:
1.2
Plugin Category:
Technical/Development
Plugin Game:
Any
Plugin Dependencies:
    Servers with this Plugin:
    10 
    Plugin Description:
    WebSocket protocol implementation to create a server via sourcemod (Version 1.2, Updated 12.01.2017)
    Old 04-11-2012 , 19:33   [DEV] WebSocket Server - Direct connection between webbrowser and gameserver
    Reply With Quote #1

    WebSocket
    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.

    Code is on github!
    https://github.com/peace-maker/sm-websocket

    Acknowledgements
    This implementation follows the draft version 17 of the websocket protocol specifications, except:
    • Only partial support for fragmented messages. The plugin can handle incoming fragmented messages and reassemble them, but doesn't split big outgoing messages.
    • 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

    Natives

    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.
     */
    native 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 callback
     * @param we                The error callback
     * @return                    True if child socket was hooked, false otherwise
     */
    native bool:Websocket_HookChild(WebsocketHandle:childwebsocketWebsocketReceiveCB:recvWebsocketDisconnectCB:discWebsocketErrorCB:we);

    /**
     * Hooks child socket's readystate changes
     *
     * @param childwebsocket    The child websocket to hook.
     * @param readystate               ReadyState change callback
     * @return                    True if child socket was hooked, false otherwise
     */
    native bool:Websocket_HookReadyStateChange(WebsocketHandle:childwebsocketWebsocketReadyStateChangedCB:readystate);

    /**
     * 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 \0 might be used before the end of the data.
     * @return                    True if child socket was hooked, false 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(WebsocketHandle: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()!
     *
     * @param websocket    The master websocket
     * @noreturn
     */
    native Websocket_Close(WebsocketHandle:websocket); 
    Requirements
    Compiling dependencies


    Thanks to
    • sfPlayer and his Socket extenstion
    • SirLamer - Base64 include

    Changelog
    Spoiler


    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 - 787 views - 48.1 KB)
    File Type: smx websocket.smx (22.2 KB, 998 views)
    File Type: inc websocket.inc (6.2 KB, 1419 views)
    __________________

    Last edited by Peace-Maker; 01-12-2017 at 21:56. Reason: Released version 1.2
    Peace-Maker is offline
     


    Thread Tools
    Display Modes

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off

    Forum Jump


    All times are GMT -4. The time now is 16:29.


    Powered by vBulletin®
    Copyright ©2000 - 2024, vBulletin Solutions, Inc.
    Theme made by Freecode