[Library] Websocket Asio with non-ssl only

Post your working scripts, libraries and tools for AHK v1.1 and older
PhonexEric24
Posts: 2
Joined: 21 Sep 2020, 01:09
Contact:

[Library] Websocket Asio with non-ssl only

21 Sep 2020, 01:19

Github link: https://github.com/pdckxd/WebSocketAsioAHK

Introduce
  • This project provides several functions for AutoHotKey to connect to server as websocket client.
  • WebSocketAsio-[x86|x64].dll should be only compatible with AutoHotKey unicode installation. (Not tested with AutoHotKey ANSI version)
  • WebSocketAsio dll is based on boost beast library.
  • Static link with runtime library so it should support win7/win8/win10
  • Provide both x86 & x64 dll. See bin folder for pre-built binaries.
  • Check Samples folder for usage.
2. Motivation
I need websocket APIs to write a GUI AutoHotKey script for controlling and monitoring OBS application through websocket protocol. I find there is only 1 suitable websocket client library for AutoHotKey which supports non-ssl websocket (https://github.com/agrippa1994/WebSocket-API). But it only supports ANSI 32bit AutoHotKey which can't call functions in bcrypt.dll. So I refer to websocket async client example of boost beast library to create this AHK module. Another implementation (https://github.com/G33kDude/WebSocket.ahk) doesn't support non-ssl.

3. Backlog
  • 1. [ ] Support wss (ssl websocket)
  • 2. [ ] Enhancement error handling
4. Known issue/Limitation
  • Windows only since AutoHotKey only supports windows
  • Sometimes you will get error "The I/O operation has been aborted because of either a thread exit or an application request" when calling websocketConnect again.
  • on_disconnected callback gets called only when user actively calls websocketDisconnect. It won't get called if unexpected error occurs such as server close the connection but on_fail callback can handle them.
5. API list
  • int websocketRegisterOnConnectCb(callbackName)
    Desc: Register a function defined in user's AHK script to be called after connection is successfully established.
    @callbackName string: user defined function without parameter in AHK script. e.g. on_connect(){}
    @return int: 0 success, 1 failed
  • int websocketRegisterOnDataCb(callbackName)
    Desc: Register a function defined in user's AHK script to be called after data from server reaches client.
    @callbackName string: user defined function with 2 parameters in AHK script. e.g. on_data(data, len){}
    @return int: 0 success, 1 failed
  • int websocketRegisterOnFailCb(callbackName)
    Desc: Register a function defined in user's AHK script to be called whenever websocket error occurs
    @callbackName string: user defined function with 1 parameter in AHK script. e.g. on_fail(from){}
    @return int: 0 success, 1 failed
  • int websocketRegisterOnDisconnectCb(callbackName)
    Desc: Register a function defined in user's AHK script to be called after user actively calls websocketDisconnect() function
    @callbackName string: user defined function without parameter in AHK script. e.g. on_disconnected(){}
    @return int: 0 success, 1 failed
  • int websocketEnableVerbose(nEnabled)
    Desc: Enable or disable verbose output from WebsocketAsio-[x86|x64].dll internally
    @nEnable int: 0 disable, 1 enable
    @return int: 0 success, 1 failed
  • int websocketConnect(websocketUri)
    Desc: Connect to websocket server. on_connect callback gets called if the function is registered by calling websocketRegisterOnConnectCb(callbackName)
    @websocketUri string: websocket uri. e.g. ws://localhost:8199/ws
    @return int: 0 success, 1 failed
  • int websocketDisconnect()
    Desc: Close connection to websocket server. on_disconnected callback gets called if the function is registered by calling websocketRegisterOnDisconnectCb(callbackName)
    @return int: 0 success, 1 failed
  • int websocketIsConnected()
    Desc: Get connection status
    @return int: 0 closed, 1 connected
  • int websocketSendData(requestPayload)
    Desc: Send data to remote websocket server
    @requestPayload string: request payload to send to the server
    @return int: 0 success, 1 failed
6. Welcome any feedback or pull request of bug fix and feature enhancement
User avatar
elModo7
Posts: 217
Joined: 01 Sep 2017, 02:38
Location: Spain
Contact:

Re: [Library] Websocket Asio with non-ssl only

21 Sep 2020, 04:14

I use GeekDude's websocket with obs for scene info/switching, show/hide sources etc.
It does the job quite well in my experience.
PhonexEric24
Posts: 2
Joined: 21 Sep 2020, 01:09
Contact:

Re: [Library] Websocket Asio with non-ssl only

21 Sep 2020, 04:41

elModo7 wrote:
21 Sep 2020, 04:14
I use GeekDude's websocket with obs for scene info/switching, show/hide sources etc.
It does the job quite well in my experience.
Good to hear. Seems GeekDude's websocket lib reports error on my OS. Not sure what's going on. Anyway, now we have many alternative ways to achieve.
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: [Library] Websocket Asio with non-ssl only

21 Sep 2020, 08:23

Can we get a quick Pro/Con list of each method? (Yours and G33kdudes)

Looks interesting though, thanks!
JackbobEvolved
Posts: 4
Joined: 06 Jan 2021, 17:44

Re: [Library] Websocket Asio with non-ssl only

06 Jan 2021, 17:49

elModo7 wrote:
21 Sep 2020, 04:14
I use GeekDude's websocket with obs for scene info/switching, show/hide sources etc.
It does the job quite well in my experience.
ElModo7, do you have any example scripts? I’ve been trying to build an AHK script to run logic for dynamic moving and positioning of items, but I can’t find any example scripts of the syntax to get a command into OBS using GeekDude. I don’t know AHK well (at all), but I do enough programming to in C++ & Swift that I’m confident I can Frankenstein something if I’m able to just get the websocket working.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: kunkel321 and 235 guests