WebSocket.ahk

Post your working scripts, libraries and tools for AHK v1.1 and older
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

WebSocket.ahk

28 Jul 2017, 08:00

WebSocket.ahk

Connect to a WebSocket server using AutoHotkey by taking advantage of a hidden ActiveX control.
Note that it does not garbage collect until disconnected.


Usage

Code: Select all

#NoEnv
SetBatchLines, -1

#Include ../WebSocket.ahk

new Example("wss://echo.websocket.org/")
return


class Example extends WebSocket
{
	OnOpen(Event)
	{
		InputBox, Data, WebSocket, Enter some text to send through the websocket.
		this.WebSock.Send(Data)
	}
	
	OnMessage(Event)
	{
		MsgBox, % "Received Data: " Event.data
		this.Close()
	}
	
	OnClose(Event)
	{
		MsgBox, Websocket Closed
		this.Disconnect()
	}
	
	OnError(Event)
	{
		MsgBox, Websocket Error
	}
	
	__Delete()
	{
		MsgBox, Exiting
		ExitApp
	}
}

Related Projects

Download
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: WebSocket.ahk

14 Oct 2017, 11:46

Thanks! Much easier to use than some of the other WebSocket examples I found. The code is very simple and it works great. :)

I'm using AHKv2 and I only had to switch the + to a . here to make it work for me. I think that change should be better cross-compatible across AHK versions, yes? If so, might be worth making on your master.

Code: Select all

	; Called by the JS in response to WS events
	_Event(EventName, Event)
	{
		this["On" + EventName](Event)
	}
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: WebSocket.ahk

15 Oct 2017, 14:36

I got my AHK and JS mixed up, haha! Does it work in v2 if you use ["On" EventName] or is the . required?
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: WebSocket.ahk

15 Oct 2017, 14:45

It also works without the dot, yes. (I just tend to use it for personal preference)
kevindevm
Posts: 4
Joined: 15 Apr 2019, 23:03

Re: WebSocket.ahk

05 Jun 2019, 16:12

so i use this for a trade site for a game, and all work out but i only can have 6 websocket open when i try to open other one i get this
56619708-d7e84c00-65fc-11e9-8dc8-f327ff4738a1.png
56619708-d7e84c00-65fc-11e9-8dc8-f327ff4738a1.png (14.48 KiB) Viewed 12903 times
basically it say error line 1 character 1 code 0 no url and if i want to continue it will not open the socket, any chance about why this happen? thanks for this
eL Tux
Posts: 66
Joined: 06 Nov 2015, 07:52

Re: WebSocket.ahk

19 Jul 2020, 10:03

I try to use this script but I can't find how to send more than 1 message, when I send more than 1 message I got echo.
Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Re: WebSocket.ahk

23 Jul 2020, 06:25

Hi,

I'm trying to use this library to connect to a websocket API. Outside of AHK I have been able to successfully connect to the websocket by issuing the following the HTML request:

Code: Select all

GET / HTTP/1.1
Host: 192.168.1.185:45000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
Upgrade: websocket
Origin: http ://192.168.1.185:45000
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en-GB;q=0.9,en;q=0.8
Sec-WebSocket-Key: bMCW4pNf4bMWxSyKKgd3bQ==
Sec-WebSocket-Protocol: rotski

How do I go about using this class to issue the above request and connect to the websocket server using AHK?
Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Re: WebSocket.ahk

24 Jul 2020, 11:20

Disregard my previous post. I have discovered that the server requires that rotski is specified as the subprotocol when connecting to the websocket.

Using the "Browser WebSocket Client" plugin for chrome, for example, I can achieve a successful connection to ws://localhost:45000 when specifying this subprotocol.

Is this possible to achieve with this WebSocket AHK class?
Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Re: WebSocket.ahk

24 Jul 2020, 13:17

I've now managed to work out how to specify a subprotocol and connect successfully, but the connection seems to timeout after 30s. Any way to make the connection persistent?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: WebSocket.ahk

19 Feb 2022, 08:53

@geek - is your tool still supported? :think:
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: WebSocket.ahk

19 Feb 2022, 09:03

BoBo wrote:
19 Feb 2022, 08:53
@geek - is your tool still supported? :think:
It still works in Win10 and Win11 to my knowledge, but as far as support goes I've never provided any.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: WebSocket.ahk

19 Feb 2022, 09:07

:thumbup: Came to this page via GitHub and your Chrome.ahk tool and have read somewhere in a thread that it's already 4 yrs available, so there's always kinda concern if such amazing scripts are still running/compatible. Thx for your effort 8-)
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: WebSocket.ahk

09 Mar 2023, 11:54

Hi, I'm using this in one of my projects but have one or two users who just get the websocket error when running the script (works great for everyone else otherwise).

How do I capture error logs if OnError(Event) fires? I've tried looking into the event object (is it an object?) when this fires, and I can't figure out how to get the data from it. I also can't seem to get it to error out on my end, so if you have any suggestions on:

1) Gathering the error data when OnError() is called

2) Inducing OnError to fire so I can test it

3) Any reasons why OnError fires in general? These users are both on Win10+

Thanks!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: sanmaodo and 113 guests