class / function / send Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Posts: 25
Joined: 29 May 2018, 01:12

class / function / send

14 Jun 2018, 06:11

I found a websocket and it works very good.

Code: Select all

class WebSocket
		static wb
		; Create an IE instance
		Gui, +hWndhOld
		Gui, New, +hWndhWnd
		this.hWnd := hWnd
		Gui, Add, ActiveX, vWB, Shell.Explorer
		Gui, %hOld%: Default
		; Write an appropriate document
		WB.Navigate("about:<!DOCTYPE html><meta http-equiv='X-UA-Compatible'"
		. "content='IE=edge'><body></body>")
		while (WB.ReadyState < 4)
			sleep, 50
		this.document := WB.document
		; Add our handlers to the JavaScript namespace
		this.document.parentWindow.ahk_savews := this._SaveWS.Bind(this)
		this.document.parentWindow.ahk_event := this._Event.Bind(this)
		this.document.parentWindow.ahk_ws_url := WS_URL
		; Add some JavaScript to the page to open a socket
		Script := this.document.createElement("script")
		Script.text := "ws = new WebSocket(ahk_ws_url);`n"
		. "ws.onopen = function(event){ ahk_event('Open', event); };`n"
		. "ws.onclose = function(event){ ahk_event('Close', event); };`n"
		. "ws.onerror = function(event){ ahk_event('Error', event); };`n"
		. "ws.onmessage = function(event){ ahk_event('Message', event); };"
	; Called by the JS in response to WS events
	_Event(EventName, Event)
		this["On" EventName](Event)
	; Sends data through the WebSocket
	; Closes the WebSocket connection
	Close(Code:=1000, Reason:="")
	{, Reason)
	; Closes and deletes the WebSocket, removing
	; references so the class can be garbage collected
		if this.hWnd
			Gui, % this.hWnd ": Destroy"
			this.hWnd := False
open a new connection:
new conn_to_some_api("wss://api....")

Code: Select all

class conn_to_some_api extends WebSocket
... connect me ...

if returned data contains "bla"...

Code: Select all

do stuff
What I'm trying to do is to run a function based on the server response and send new data to the server.
If I try to do "this.send(data)" from inside func123 nothing is happening but if I use a "return data" which brings me back to the OnMessage(Event) of the class its working. But I need to send the data from the func123 part as there are sometimes multiple things to send.
How may I achive this? Could anyone help me out?

Posts: 295
Joined: 05 Feb 2017, 00:03

Re: class / function / send  Topic is solved

14 Jun 2018, 08:05

Is func123() part of the conn_to_some_api class?

If it isn't then the this object doesn't exist inside, and so as a solution you could pass it as a parameter:
make the function be func123(obj) and use obj.send(data) to send the data (can probably still call it 'this' but that may be confusing since it wouldn't be in the class)

call it with: func123(this) from within your OnMessage(Event) function

If it is within the class then all you should need to do is call it using this:
this.func123() within the OnMessage(Event) function

Hope that made sense, let me know if you have any luck.
User avatar
Posts: 25
Joined: 29 May 2018, 01:12

Re: class / function / send

14 Jun 2018, 10:46

Thank you :)
call it with: func123(this) from within your OnMessage(Event) function
Did the trick :)

Return to “Ask For Help”

Who is online

Users browsing this forum: Elgin, Google [Bot], Helgef, Marcosa1020, supplydrop and 45 guests