WebSocket.ahk

Post your working scripts, libraries and tools
geek
Posts: 918
Joined: 02 Oct 2013, 22:13
GitHub: G33kDude
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: 918
Joined: 02 Oct 2013, 22:13
GitHub: G33kDude
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 9066 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: 6475
Joined: 13 May 2014, 17:15

Re: WebSocket.ahk

19 Feb 2022, 08:53

@geek - is your tool still supported? :think:
geek
Posts: 918
Joined: 02 Oct 2013, 22:13
GitHub: G33kDude
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: 6475
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-)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: safetycar, t3h m00kz and 18 guests