Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: WebSocket client support in toolkit #341

Open
justparking opened this issue Sep 19, 2024 · 1 comment
Open

Feature request: WebSocket client support in toolkit #341

justparking opened this issue Sep 19, 2024 · 1 comment
Labels
feature request proposal A feature or update proposal where feedback is sought.

Comments

@justparking
Copy link
Contributor

There are increasingly more devices that support WebSockets to control them (e.g. the WLED project)

It's a protocol that lends itself to Nodel recipes nicely and would make a very handy addition to the Toolkit.

I've been looking at some very lightweight, simple libraries and these two look promising:

  1. https://github.com/gusavila92/java-android-websocket-client
  2. https://github.com/TooTallNate/Java-WebSocket

Either could be cleanly integrated into Nodel making use of the relevant threading and logging frameworks.

@justparking justparking added feature request proposal A feature or update proposal where feedback is sought. labels Sep 19, 2024
@justparking
Copy link
Contributor Author

I've started on this. Making some notes for initial interfaces, for the record:

nodetoolkit.py

def WebSocket(dest=None, opened=None, textReceived=None, binaryReceived=None, pingReceived=None, pongReceived=None, exceptionReceived=None, closed=None):
    return nodetoolkit.createWebSocket(dest, opened, textReceived, binaryReceived, pingReceived, pongReceived, exceptionReceived, closed)
    # ...

ManagedToolkit.java

    // ...
    public ManagedWebSocket createWebSocket(
                                String dest,
                                H0 opened,
                                H1<String> textReceived,
                                H1<byte[]> binaryReceived,
                                H1<byte[]> pingReceived,
                                H1<byte[]> pongReceived,
                                H1<Exception> exceptionReceived,
                                H2<Integer, String> closed) {
    // ...

example script.py

def ws_opened():
  console.info("ws_opened")
  _ws.send("hello from nodel")
  
def ws_textReceived(text):
  console.info("ws_textReceived %s" % text)
  
def ws_binaryReceived(data):
  console.info("ws_binaryReceived %s" % data)
  
def ws_pingReceived(data):
  console.info("ws_pingReceived %s" % data)  

def ws_pongReceived(data):
  console.info("ws_pongReceived %s" % data)

def ws_exceptionReceived(exc):
  console.info("ws_exceptionReceived %s" % exc)

def ws_closed(i, msg):
  console.info("ws_closed %s %s" % (i, msg))

_ws = WebSocket(  opened=ws_opened,
                  textReceived=ws_textReceived,
                  binaryReceived=ws_binaryReceived,
                  pingReceived=ws_pingReceived,
                  pongReceived=ws_pongReceived,
                  exceptionReceived=ws_exceptionReceived,
                  closed=ws_closed)

def main():
  _ws.setDest("ws://192.168.10.5:80/ws")

build.gradle

dependencies { 
    // ...
    // websocket client
    compile 'dev.gustavoavila:java-android-websocket-client:2.0.2'

My initial concerns are:

  1. can that set of callhandlers be minimised or is every one necessary
  2. what's best way to pass byte[] types through to Jython - I might just do the same as Managed UDP/TCP classes
  3. the java-android-websocket-client dependency have a very poor bespoke threading model which might required being transplanted into nodel as opposed to being used as a dependency, but need to asses other WebSocket client APIs too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request proposal A feature or update proposal where feedback is sought.
Projects
None yet
Development

No branches or pull requests

1 participant