mirror of
https://github.com/defold/extension-websocket.git
synced 2025-06-27 17:57:43 +02:00
1.7 KiB
1.7 KiB
title, brief
title | brief |
---|---|
Defold websocket extension API documentation | This manual covers how to use websockets with Defold |
Defold websocket extension API documentation
This extension supports both secure (wss://
) and non secure (ws://
) websocket connections.
All platforms should support this extension.
Here is how you connect to a websocket and listen to events:
local function websocket_callback(self, conn, data)
if data.event == websocket.EVENT_DISCONNECTED then
log("Disconnected: " .. tostring(conn))
self.connection = nil
update_gui(self)
elseif data.event == websocket.EVENT_CONNECTED then
update_gui(self)
log("Connected: " .. tostring(conn))
elseif data.event == websocket.EVENT_ERROR then
log("Error: '" .. data.error .. "'")
elseif data.event == websocket.EVENT_MESSAGE then
log("Receiving: '" .. tostring(data.message) .. "'")
end
end
function init(self)
self.url = "ws://echo.websocket.org"
local params = {}
self.connection = websocket.connect(self.url, params, websocket_callback)
end
function finalize(self)
if self.connection ~= nil then
websocket.disconnect(self.connection)
end
end
Installation
To use this library in your Defold project, add the following URL to your game.project
dependencies:
https://github.com/defold/extension-websocket/archive/master.zip
We recommend using a link to a zip file of a specific release.
Source code
The source code is available on GitHub