WebSockets for GLua
Go to file
2018-03-23 02:50:02 +01:00
examples changed way outgoing messages are processed 2018-03-07 08:47:57 +01:00
include added updated windows openssl static lib 2018-03-21 00:48:29 +01:00
libs Merge branch 'master' of https://github.com/FredyH/GWSockets 2018-03-23 02:50:02 +01:00
src fixed sockets stopping to work on linux 2018-03-23 02:47:02 +01:00
tests corrected url 2018-03-11 13:48:48 +01:00
.gitignore changed include to from <GWSocket.h> to "GWSocket.h" 2018-03-03 16:08:34 -05:00
BuildProjects.bat Initial commit 2018-03-01 00:44:44 +01:00
BuildProjects.lua added updated windows openssl static lib 2018-03-21 00:48:29 +01:00
BuildProjects.sh Added linux libraries 2018-03-03 20:02:12 +01:00
LICENSE Initial commit 2018-03-01 00:36:18 +01:00
README.md Update README.md 2018-03-07 08:51:42 +01:00
version.txt fixed websockets not working under linux 2018-03-19 12:57:44 +01:00

GWSockets

WebSockets for GLua

THIS IS STILL A WIP

Do not use this yet, many changes will be made and a prebuilt binary will be made available as soon as it is ready.

Usage

Place either gmsv_gwsockets_win32.dll (Windows) or gmsv_gwsockets_linux.dll (Linux) into you GarrysMod/lua/bin folder.

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://example.com:9999/api/socketserver"

    GWSockets.createWebSocket( url )
    
  • 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)

    function WEBSOCKET:onMessage( msg )  end
    function WEBSOCKET:onError( err ) end 
    function WEBSOCKET:onConnected() end
    function WEBSOCKET:onDisconnected() end
    
  • Lastly open the connection

    WEBSOCKET:open()
    
  • Once connected you can send messages using the write function

    WEBSOCKET:write( message )
    
  • 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

Example:

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

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. Run BuildProjects.sh or BuildProjects.bat while having premake5 in your PATH. Then use the appropriate generated solution for your system 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.

Linux

The required static libraries for linux are not included in this repository because they are usually very easy to obtain from the package manager of your distro. For example on ubuntu all you need to install is:

sudo apt-get install build-essential gcc-multilib g++-multilib
sudo apt-get install libssl-dev:i386 libboost-system-dev:i386

Then running the makefile should find all the required static libraries automatically.