Coder Social home page Coder Social logo

anotherspy / gwsockets Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fredyh/gwsockets

0.0 0.0 0.0 57.99 MB

WebSockets for GLua

License: MIT License

Shell 0.01% C++ 98.75% Perl 0.01% C 1.22% Lua 0.01% HTML 0.01% Batchfile 0.01% M4 0.01%

gwsockets's Introduction

GWSockets

WebSockets for GLua

Usage

Place either gmsv_gwsockets_win32.dll (Windows) or gmsv_gwsockets_linux.dll (Linux) into your GarrysMod/lua/bin folder. On windows you will require the Visual C++ Redistributable 2017, which you can find here.

NOTE: CentOS is currently not supported and appears to be having multiple issues. If you need the library to work on CentOS, compile it on CentOS using the instructions all the way at the bottom, but also replace the included ssl libraries with the ones provided by CentOS.

NOTE: Even though this module is mainly aimed at servers, it can also be used on clients. Just rename the module to gmcl_gwsockets_os and it will work on clientside as well.

You will also need to require the module in lua before you will be able to use it. You can do this running

require("gwsockets")

Documentation

Connecting to a socket server

  • First initialize a websocket instance using

    NOTE: URL's must include the scheme ( Either ws:// or wss:// )

    Example: "wss://echo.websocket.events/api/socketserver"

    GWSockets.createWebSocket( url, verifyCertificate=true )

    NOTE: If you want your websockets to use SSL but don't have a trusted certificate, you can set the second parameter to false.

  • If you are running certain versions of Linux (e.g. CentOS) it might be necessary to specify a different path for the root certificates. This is only required if you want to use SSL and verify set verifyCertificates to true when creating a websocket.

 GWSockets.addVerifyPath( "/etc/ssl/certs" )
  • Next add any cookies or headers you would like to send with the initial request (Optional)

    WEBSOCKET:setHeader( key, value )
    WEBSOCKET:setCookie( key, value )
  • Add some callbacks (Optional)

    -- called when a message from the peer has been received
    function WEBSOCKET:onMessage( msg )  end 
    
    -- called whenever anything goes wrong, this is always followed by a call to onDisconnected
    function WEBSOCKET:onError( errMessage ) end 
    
    -- called as soon as the socket is connected
    -- This is a good place to start sending messages
    function WEBSOCKET:onConnected() end 
    
    -- called whenever the socket has been disconnected
    -- this can either be because the socket has been requested to closed (either through user or error)
    -- or because the peer has closed the connection
    -- Note: If the peer does not close the connection gracefully, this might not be called until a write is attempted.
    function WEBSOCKET:onDisconnected() end 
  • Lastly open the connection

    WEBSOCKET:open()
  • Once the socket has been opened you can send messages using the write function

    WEBSOCKET:write( message )

    NOTE: You can write messages to the socket before the connection has been established and the socket will wait before sending them until the connection has been established. However, it is best practice to only start sending in the onConnected() callback.

  • You can close the websocket connection at any time using close OR closeNow

    WEBSOCKET:close()
    WEBSOCKET:closeNow()
    • close will wait for all queued messages to be sent and then gracefully close the connection
    • closeNow will immediately terminate the connection and discard all queued messages
  • You can cancel any queued outbound messages by calling

    WEBSOCKET:clearQueue()
  • You can check if the websocket is connected using

    WEBSOCKET:isConnected()

    NOTE: You should avoid using this and instead rely on the callbacks.

Example:

require("gwsockets")
local socket = GWSockets.createWebSocket("wss://echo.websocket.events/")

function socket:onMessage(txt)
    print("Received: ", txt)
end

function socket:onError(txt)
    print("Error: ", txt)
end

-- We start writing only after being connected here. Technically this is not required as this library
-- just waits until the socket is connected before sending, but it's probably good practice
function socket:onConnected()
    print("Connected to echo server")
    -- Write Echo once every second, 10 times
    timer.Create("SocketWriteTimer", 1, 0, function()
        print("Writing: ", "Echo")
        socket:write("Echo")
    end)
    timer.Simple(10, function()
        timer.Remove("SocketWriteTimer")
        -- Even if some of the messages have not reached the other side yet, this type of close makes sure
        -- to only close the socket once all queued messages have been received by the peer.
        socket:close()
    end)
end

function socket:onDisconnected()
    print("WebSocket disconnected")
end

socket:open()

Build

Requires premake5.

Depending on your platform run one of the following commands to create a build script:

premake5 --os=windows --file=BuildProjects.lua vs2010    # Windows
premake5 --os=linux   --file=BuildProjects.lua gmake     # Linux
premake5 --os=macosx  --file=BuildProjects.lua gmake     # Mac

Then use the appropriate generated solution for your system in the solutions/ folder and build the project.

Windows

On Windows all you need to do is open the generated visual studio project and build the dll. All libraries and headers are provided already. If you wish to build the 64 bit version you just have to switch the build configuration to x64.

Linux

On linux only essential programs for building C++ programs are required. On Ubuntu 64-bit these are:

sudo apt-get install build-essential gcc-multilib g++-multilib

The required static libraries for linux are included in this repository to avoid library/header version mismatching, but feel free to use your OS' libraries instead.

To build the project simply run

make                           # x86
make config=release_x86_64     # x64

Possible Issues

This library uses OpenSSL built for Ubuntu, which sets the default search path for root certificates to the one Ubuntu uses. There is a possibility, that this path is different on other systems. In that case you will need to swap out the libssl.a and libcrypto.a provided in this repositor with the ones provided by your operating system.

gwsockets's People

Contributors

fredyh avatar kgoodale13 avatar viral32111 avatar doridian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.