# Defold-WebSocket (DEPRECATED)

**URL:** <https://forum.defold.com/t/defold-websocket-deprecated/17109>\
**Category:** The Defoldmine\
**Created:** [February 22, 2018, 8:17pm UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109 "2018-02-22T20:17:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 22, 2018, 8:17pm UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/1 "2018-02-22T20:17:42Z")

</div>

I’ve decided to move my asynchronous WebSocket module from DefNet to a separate repo. The module has also received a number of improvements and the underlying lua-websocket project has been updated. The new version should in theory also work with secure WebSockets thanks to the use of LuaSec. Defold-WebSocket is currently used in the Defold client of the [Colyseus game server and client lib](https://forum.defold.com/t/colyseus-multiplayer-game-client-server/17101).

Check the asset page for details and an example of how to use the module:

[https://www.defold.com/community/projects/99436/](https://www.defold.com/community/projects/99436/)

---

<div class="post-metadata">

**Author:** ![joshua.winters](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@joshua.winters](https://forum.defold.com/u/joshua.winters)\
**Post date:** [June 27, 2018, 7:01am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/2 "2018-06-27T07:01:53Z")

</div>

I noticed the secure websocket does not work for HTML5 version, it indeed works for other kinds of bundles.

For HTML5 version, there is an error message like “MyProject.js:1 WebSocket connection to ‘ws://domain:port/’ failed: Error during WebSocket handshake: net::ERR\_INVALID\_HTTP\_RESPONSE”

In fact, I specified the target url '‘wss://domain:port/’ instead of ‘ws://domain:port/’. I don’t know if this problem is easy to fix since I already get openssl / luasec extension built with emscripten.

---

<div class="post-metadata">

**Author:** ![joshua.winters](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@joshua.winters](https://forum.defold.com/u/joshua.winters)\
**Post date:** [June 27, 2018, 7:08am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/3 "2018-06-27T07:08:54Z")

</div>

I also think about if I can implement the web scoket with javascript and call it from lua (for HTML5 version)?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 27, 2018, 7:16am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/4 "2018-06-27T07:16:21Z")

</div>

Hmm, I thought secure websockets worked in html5. @endel?

---

<div class="post-metadata">

**Author:** ![joshua.winters](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@joshua.winters](https://forum.defold.com/u/joshua.winters)\
**Post date:** [June 27, 2018, 7:21am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/5 "2018-06-27T07:21:28Z")

</div>

No, it does not. [luasec](https://github.com/britzl/defold-luasec/tree/master/luasec/lib) does NOT have proper openssl lib for html5, and its core functionality is comments out for html5.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 27, 2018, 6:32pm UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/6 "2018-06-27T18:32:47Z")

</div>

Hold on, for HTML5 there’s no use of LuaSec. Emscripten will automatically upgrade socket connections to Websockets. That’s why the Defold-Websocket project has two code paths in many places to for instance skip the handshake and socket upgrade etc. The readme should documentat thus fairly well.

---

<div class="post-metadata">

**Author:** ![GreenArrow](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/greenarrow/32/18490_2.png) [@GreenArrow](https://forum.defold.com/u/GreenArrow)\
**Post date:** [October 27, 2018, 6:29am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/7 "2018-10-27T06:29:17Z")

</div>

Trying to get this to work with an empty project, but I am getting a build error. Thoughts?  
Editor: 1.2.139 on Windows 10

 ![defold1](https://forum-defold.b-cdn.net/uploads/default/original/2X/f/f72602d06e0a673f5a05fd2213e2e9754235fbf4.png)  
 ![defold2](https://forum-defold.b-cdn.net/uploads/default/original/2X/e/e5e467c9ec11866bc00d8aacfeaea6f35964e191.png)

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [October 27, 2018, 6:47am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/8 "2018-10-27T06:47:47Z")

</div>

Replace your luasec with luasec by @britzl[https://github.com/britzl/defold-luasec/archive/master.zip](https://github.com/britzl/defold-luasec/archive/master.zip)  
It fixed for working with clang compiler

---

<div class="post-metadata">

**Author:** ![GreenArrow](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/greenarrow/32/18490_2.png) [@GreenArrow](https://forum.defold.com/u/GreenArrow)\
**Post date:** [October 27, 2018, 6:56am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/9 "2018-10-27T06:56:44Z")

</div>

That and after an editor restart fixed it. Thanks!

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 27, 2018, 8:17am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/10 "2018-10-27T08:17:52Z")

</div>

How did you know to put in that specific LuaSec version by sonountaleban and not the fork by me with the fix?

---

<div class="post-metadata">

**Author:** ![GreenArrow](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/greenarrow/32/18490_2.png) [@GreenArrow](https://forum.defold.com/u/GreenArrow)\
**Post date:** [October 27, 2018, 8:31am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/11 "2018-10-27T08:31:55Z")

</div>

I actually grabbed the incorrect link from your page:  
[https://github.com/britzl/defold-luasec](https://github.com/britzl/defold-luasec)

 ![defold3](https://forum-defold.b-cdn.net/uploads/default/original/2X/2/2a7510780450d0cb5317ae91abf22259ad8b2ecb.png)

Didn’t realized it was pointing to a different link until @AGulev pointed it out.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 27, 2018, 8:34am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/12 "2018-10-27T08:34:39Z")

</div>

Ah, I see. Well, it’s a fork and I didn’t think to edit the readme. Fixed now!

---

<div class="post-metadata">

**Author:** ![GreenArrow](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/greenarrow/32/18490_2.png) [@GreenArrow](https://forum.defold.com/u/GreenArrow)\
**Post date:** [October 27, 2018, 8:41am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/13 "2018-10-27T08:41:52Z")

</div>

Great stuff btw.

I’m just testing the basics right now, so far I’m getting connected properly and able to send messages back and forth.

However, I don’t seem to be getting an error event when the client can’t connect to the server? Like if the server is offline. How can I capture this?

Also, if I terminate the server abruptly, I’m also not getting a disconnected event?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 27, 2018, 10:45am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/14 "2018-10-27T10:45:07Z")

</div>

You should be able to catch errors in the client. I believe Colyseus does this.

---

<div class="post-metadata">

**Author:** ![COCO](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/coco/32/38692_2.png) [@COCO](https://forum.defold.com/u/COCO)\
**Post date:** [November 14, 2018, 6:09am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/15 "2018-11-14T06:09:27Z")

</div>

Does it support html5 wasm projects exported by Defold?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 14, 2018, 6:20am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/16 "2018-11-14T06:20:46Z")

</div>

Yes, sure, I just tried the example project and it connects as expected. Any reason why it shouldn’t work with web assembly?

---

<div class="post-metadata">

**Author:** ![COCO](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/coco/32/38692_2.png) [@COCO](https://forum.defold.com/u/COCO)\
**Post date:** [November 14, 2018, 6:24am UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/17 "2018-11-14T06:24:00Z")

</div>

Great, great!  
I just new to Defold so…  
Anyway thanks for reply.

---

<div class="post-metadata">

**Author:** ![GreenArrow](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/greenarrow/32/18490_2.png) [@GreenArrow](https://forum.defold.com/u/GreenArrow)\
**Post date:** [November 23, 2018, 4:33pm UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/18 "2018-11-23T16:33:30Z")

</div>

With an empty clean project, and this code based on the example page:

```lua
local client_async = require "websocket.client_async"

function init(self)
	self.ws = client_async()
	self.ws:on_connected(function(ok, err)
		if ok then
			print("Connected")
			msg.post("#", "acquire_input_focus")
		else
			print("Unable to connect", err)
		end
	end)
	self.ws:on_disconnected(function()
		print("Disconnected")
	end)
	self.ws:connect("ws://localhost:8080")
end

function update(self, dt)
	self.ws:step()
end

```

It prints the message properly if the server is running. But if the server is not running, the “Unable to connect” does not get printed.

I also tried Colyseus, and it’s the same thing. The “on error” event there also doesn’t get triggered.

I tried it on the latest editor 1.2.142, but I’ve never been successful in older versions either. I also tried html5 build, I do get a websocket error there.  
 ![defold5](https://forum-defold.b-cdn.net/uploads/default/original/2X/6/6c9dc1ac50da9885779ee119b0de5922a2574fed.png)

However, this error is not captured in code (i.e. can’t connect error doesn’t even get printed in the console).

How do I capture this connection refused properly in code, so I can display an error message properly? Potential workaround is to have a timeout e.g. if after 2 seconds I am still not connected, then I can assume something is wrong with the connection. But I am hoping this is something the extension is capturing properly, and I just don’t know how to make it work.

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [November 23, 2018, 6:44pm UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/19 "2018-11-23T18:44:30Z")

</div>

> [@GreenArrow](#):
>
> I also tried Colyseus, and it’s the same thing. The “on error” event there also doesn’t get triggered.

If you are using Colyseus, you should not to connect immediately: [client.onError doesn't triggered when there is no connection/server · Issue #6 · colyseus/colyseus-defold · GitHub](https://github.com/gamestdio/colyseus-defold/issues/6#issuecomment-427810381)

Strange but I just test your code with [example](https://github.com/britzl/defold-websocket/tree/master/examples) project. It prints the error message on desktop(mac) if the server is not running.

Could you please `pprint` or debug the `err` and `ok`?

Also it is better to use `err`

```lua
if err then
  print("Unable to connect", err)
end

```

 ![21](https://forum-defold.b-cdn.net/uploads/default/original/2X/3/3cd2298f49e48051b11db9d3fc20454e08def10c.png)

---

<div class="post-metadata">

**Author:** ![GreenArrow](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/greenarrow/32/18490_2.png) [@GreenArrow](https://forum.defold.com/u/GreenArrow)\
**Post date:** [November 23, 2018, 7:26pm UTC](https://forum.defold.com/t/defold-websocket-deprecated/17109/20 "2018-11-23T19:26:28Z")

</div>

I’ve added the pprint you suggested.

Here’s what I get if the server is NOT running :

 ![defoldconnect1](https://forum-defold.b-cdn.net/uploads/default/original/2X/5/5fd27b8363670da9254839bad20e79864be5e622.png)

For comparison, here’s what I get if the server is running.

 ![defoldconnect2](https://forum-defold.b-cdn.net/uploads/default/original/2X/2/2dc126f9757bfcf4459a33a67d69b62ff5128000.png)

I am using Windows 10. I don’t have access to a Mac right now, but I will try over there when I get the chance. Actually, I am also not getting the Disconnected event (if I terminate the server while the Defold client is connected).

For Colyseus, yes my set-up is the same (call to connect is after the listeners have been registered), but sadly the same results.

[Next page](https://forum.defold.com/t/defold-websocket-deprecated/17109.md?page=2)
