ANN: dmc_websockets - WebSocket module for Corona SDK

i just published a WebSocket module to my Corona repo at github. it’s one of the tools i needed for a project i’m working on, so decided to share it with the community.

i tested the performance a few days ago and was getting ~2-3KB/s throughput – the example i have showed the data updates reflected in a scrollable list (scrolling smoothly). soon i’ll post the code for this performance example in the github repo along with the other example currently there.

that throughput is fast enough for my project, however i’m sure it could be faster. unfortunately Corona SDK doesn’t include any of the binary processing libraries (bit or lpack), so everything is being manipulated in Lua. :frowning:

it would be awesome if CoronaLabs or someone in the community added these two libraries to the Plugins area so that we could supercharge low-level binary protocols ! :slight_smile:

if you’re interested to know more:

http://code.coronalabs.com/code/dmcwebsockets

https://github.com/dmccuskey/DMC-Corona-Library

happy coding !

dmc

ps, i actually wrote this to support a library module for WAMP (http://wamp.ws) – a Corona WAMP client is really the Holy Grail for my project. i’m going to start coding soon…

If i understand you correctly, your looking for a bit library? If so, have you seen this? http://docs.coronalabs.com/daily/plugin/bit/

sweet ! i just looked over the plugins last week and must have gone right past it.

thank you for letting me know. i’m going to integrate it ASAP !

cheers,

dmc

Welcome :slight_smile:

Also, thanks for sharing this library!

i integrated the ‘bit’ plugin into the websocket lib and then ran a simple test to see how much faster it ran. looks like the plugin is around 20% faster. w00t !

Glad it helped :slight_smile:

If i understand you correctly, your looking for a bit library? If so, have you seen this? http://docs.coronalabs.com/daily/plugin/bit/

sweet ! i just looked over the plugins last week and must have gone right past it.

thank you for letting me know. i’m going to integrate it ASAP !

cheers,

dmc

Welcome :slight_smile:

Also, thanks for sharing this library!

i integrated the ‘bit’ plugin into the websocket lib and then ran a simple test to see how much faster it ran. looks like the plugin is around 20% faster. w00t !

Glad it helped :slight_smile:

Hi,

Ready to implement dmc_websockets. Starting a new project.

I have installed the files and directories per docs.  The following are in the root directory.

dmc_corona (folder)

dmc_corona.cfg

dmc_corona_boot.lua

At the moment the main.lua file is really simple:

local storyboard = require "storyboard" local WebSockets = require 'dmc\_library.dmc\_websockets' 

I get the following error:

File: module ‘dmc_library.dmc_websockets’ not found

Module ‘dmc_library.dmc_websockets’ not found:resource (dmc_library.dmc_websockets.lu) does not exist in archive

I am working as a Starter for the moment.  Is that my problem?

Thanks,

–Scot

hey Scot,

the websockets import should be:

local WebSockets = require 'dmc\_corona.dmc\_websockets'

(i.e., dmc_corona instead of dmc_library, according to how you’ve organized the project)

i’ll check the docs to make sure that all of that is correct, i might have missed something after the library name change.

cheers, dmc

That did the trick.  Thanks.

I’m connecting to a pretty wonky setup through the web socket.  The “server” is actually a NetLogo model and HubNet networking with a web socket extension.  I don’t have the complete picture of what is happening on the other end.  So there is some reverse engineering required.

Here’s what know.  

All data to/from the server is JSON encoded.  It looks as if your library uses JSON.  Correct?

There is some sort of handshaking sequence taking place.  All I have to go on is a Javascript implementation of a client, so I will be attempting to mirror that sequence through the websocket.

Any thoughts or gotcha’s that come to mind would be helpful to know.

Looks great so far.

hey Scot,

the websocket protocol supports text or binary data – so yes, JSON works as text but it’s not a “native” data type, which means that you’ll have to encode/decode JSON yourself when you send/receive messages.

as long as you can reverse-engineer the JSON protocol and implement in your code, websockets will take care of transporting the JSON strings.

cheers, dmc

Still struggling.

local WebSocket = require "dmc\_corona.dmc\_websockets" local ws = WebSocket:new({uri = "192.168.1.73:9999"}) ws:send("NetLogo 5.0.2")

Getting:

2014-08-15 18:50:00.255 Corona Simulator[3869:507] Runtime error

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_sockets/async_tcp.lua:191: calling ‘send’ on bad self (tcp{client} expected, got userdata)

stack traceback:

[C]: in function ‘send’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_sockets/async_tcp.lua:191: in function ‘send’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_websockets.lua:660: in function ‘_sendFrame’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_websockets.lua:775: in function ‘_processMessageQueue’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_websockets.lua:747: in function ‘_addMessageToQueue’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_websockets.lua:737: in function ‘_sendMessage’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_websockets.lua:727: in function ‘_sendText’

/Users/scotsoutherland/Documents/Development/Websocket/dmc_corona/dmc_websockets.lua:336: in function ‘send’

/Users/scotsoutherland/Documents/Development/Websocket/main.lua:13: in main chunk

hi Scot,

everything is asynchronous, so you have to wait until the websocket is connected to send any data. because WebSocket:new() doesn’t block, what you have written will try to send a message right away without ensuring there is a connection available.

to better understand the required async-connection flow, i would take a look at the examples which are in the library. i think the simple ‘echo’ example would make a good starting point for any app. 

hth, dmc

Yep, that helps.  Looks like everything needs to happen in the event loop on the client side.  Had to compile a new NetLogo extension on the Java side (scala).  Now I have an active port on the other end and I’m getting an ONOPEN event.  So the socket object seems to be “real” now.

Looks like your part is working as designed.  Great stuff!  Thanks.  Took a look at doing this myself about 6 months ago.  SOOOOO glad you did it for us.

Now for the undocumented handshaking with HubNet.

Thanks,

–Scot

Sending a JSON string and getting and ONERROR event.  Does the the event table contain any information besides type = “ONERROR”?  

The API docs on your site indicate this is due to a communication error.  Could this be a response from the server that the message does not make sense, or does this mean that the message never made it to the server?  Or is there a difference.

Thanks,

–Scot

there is a flag near the top of dmc_websockets.lua, called LOCAL_DEBUG. set to *true* will print out more info regarding the communication process to give a better idea when things go badly. tcp.lua and async_tcp.lua have the same.

usually there will be additional info in the event table on an error – properties ‘code’ and ‘reason’. are you seeing a traceback along with the error ?

i’m not sure when you grabbed your copy of the library. i know i have made some updates recently which output more information in certain error conditions. i have also published other updates within the past few days.

if a server doesn’t understand something it is supposed to close the connection cleanly with an error code/reason, not just drop it. of course, that’ll depend on the WS implementation of the server code. :slight_smile:

as far as onError, there isn’t a lot in the WS spec regarding when to throw that, i am thinking the only reasons to throw that would be an 1. internal error (like some Lua issue) or 2. the network connection was suddenly lost.

cheers,

dmc

ps, if you’re still having issues, i can take a look at your project if you are able to send it to me.