{"id":15722619,"url":"https://github.com/britzl/defold-websocket","last_synced_at":"2025-05-13T01:25:33.850Z","repository":{"id":151986237,"uuid":"122032310","full_name":"britzl/defold-websocket","owner":"britzl","description":"DEPRECATED - Use https://github.com/defold/extension-websocket instead!","archived":false,"fork":false,"pushed_at":"2020-10-29T22:13:22.000Z","size":67,"stargazers_count":25,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-01T05:34:50.067Z","etag":null,"topics":["defold","defold-library","lua","websocket","websocket-client"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/britzl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-19T07:49:49.000Z","updated_at":"2025-02-26T07:10:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"3bc433a3-901c-42e4-8509-4713524c0dfb","html_url":"https://github.com/britzl/defold-websocket","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefold-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefold-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefold-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/britzl%2Fdefold-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/britzl","download_url":"https://codeload.github.com/britzl/defold-websocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253852298,"owners_count":21973899,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["defold","defold-library","lua","websocket","websocket-client"],"created_at":"2024-10-03T22:08:39.302Z","updated_at":"2025-05-13T01:25:33.829Z","avatar_url":"https://github.com/britzl.png","language":"Lua","readme":"![](logo.png)\n\n# THIS REPOSITORY AND DEFOLD LIBRARY IS DEPRECATED!\n\nPlease migrate from this repo to the official WebSocket implementation for Defold:\n\nhttps://github.com/defold/extension-websocket\n\n\u003cdetails\u003e\u003csummary\u003eDOCUMENTATION FOR THIS REPO\u003c/summary\u003e\n\u003cp\u003e\n\n# Defold-WebSocket\nThis project aims to provide a cross platform asynchronous implementation of the WebSockets protocol for Defold projects. Defold-WebSocket is based on the [lua-websocket](https://github.com/lipp/lua-websockets) project with additional code to handle WebSocket connections for HTML5 builds. The additional code is required since Emscripten (which is used for Defold HTML5 builds) will automatically upgrade normal TCP sockets connections to WebSocket connections. Emscripten will also take care of encoding and decoding the WebSocket frames. The WebSocket implementation in this project will bypass the handshake and frame encode/decode of lua-websocket when running in HTML5 builds.\n\n\n# Installation\nYou can use the modules from this project in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your game.project file and in the `dependencies` field under `project` add:\n\n\thttps://github.com/britzl/defold-websocket/archive/master.zip\n\nOr point to the ZIP file of a [specific release](https://github.com/britzl/defold-websocket/releases).\n\n## Dependencies\nThis project depends on the LuaSocket and LuaSec projects:\n\n* [defold-luasocket](https://github.com/britzl/defold-luasocket)\n* [defold-luasec](https://github.com/britzl/defold-luasec)\n\nYou need to add these as dependencies in your game.project file, along with the dependency to this project itself.\n\n\n# Usage\n\n```lua\nlocal client_async = require \"websocket.client_async\"\n\nfunction init(self)\n\tself.ws = client_async({\n\t\tconnect_timeout = 5, -- optional timeout (in seconds) when connecting\n\t})\n\n\tself.ws:on_connected(function(ok, err)\n\t\tif ok then\n\t\t\tprint(\"Connected\")\n\t\t\tmsg.post(\"#\", \"acquire_input_focus\")\n\t\telse\n\t\t\tprint(\"Unable to connect\", err)\n\t\tend\n\tend)\n\n\tself.ws:on_disconnected(function()\n\t\tprint(\"Disconnected\")\n\tend)\n\n\tself.ws:on_message(function(message)\n\t\tprint(\"Received message\", message)\n\tend)\n\n\tself.ws:connect(\"ws://localhost:9999\")\nend\n\nfunction update(self, dt)\n\tself.ws:step()\nend\n\nfunction on_input(self, action_id, action)\n\tif action_id == hash(\"fire\") and action.released then\n\t\tself.ws:send(\"Some data\")\n\tend\nend\n```\n\n## How to connect using a Secure Web Socket (wss://)\nWhen connecting to a secure web socket you need to specify the protocol as \"wss\" and pass SSL parameters like this when connecting the web socket:\n\n```lua\n\tlocal sslparams = {\n\t\tmode = \"client\",\n\t\tprotocol = \"tlsv1_2\",\n\t\tverify = \"none\",\n\t\toptions = \"all\",\n\t}\n\tself.ws:connect(\"wss://echo.websocket.org\", \"wss\", sslparams)\n```\n\n# Important note on Sec-WebSocket-Protocol and Chrome\nEmscripten will create WebSockets with the Sec-WebSocket-Protocol header set to \"binary\" during the handshake. Google Chrome expects the response header to include the same Sec-WebSocket-Protocol header. Some WebSocket examples and the commonly used [Echo Test service](https://www.websocket.org/echo.html) does not respect this and omits the response header. This will cause WebSocket connections to fail during the handshake phase in Chrome. Firefox does impose the same restriction. I'm not sure about other browsers.\n\n\n# Testing using a Python based echo server\nThere's a Python based WebSocket echo server in the tools folder. The echo server is built using the [simple-websocket-server](https://github.com/dpallot/simple-websocket-server) library. Start it by running `python websocketserver.py` from a terminal. Connect to it from `localhost:9999`. The library has been modified to return the Sec-WebSocket-Protocol response header, as described above.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbritzl%2Fdefold-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbritzl%2Fdefold-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbritzl%2Fdefold-websocket/lists"}