class / function / send Topic is solved

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

class / function / send

Post by Guhl » 14 Jun 2018, 06:11

I found a websocket and it works very good.

Code: Select all


class WebSocket
{
	__New(WS_URL)
	{
		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); };"
		this.document.body.appendChild(Script)
	}
	
	; Called by the JS in response to WS events
	_Event(EventName, Event)
	{
		this["On" EventName](Event)
	}
	
	; Sends data through the WebSocket
	Send(Data)
	{
		this.document.parentWindow.ws.send(Data)
	}
	
	; Closes the WebSocket connection
	Close(Code:=1000, Reason:="")
	{
		this.document.parentWindow.ws.close(Code, Reason)
	}
	
	; Closes and deletes the WebSocket, removing
	; references so the class can be garbage collected
	Disconnect()
	{
		if this.hWnd
		{
			this.Close()
			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
{
	OnOpen(Event)
	{
... connect me ...
	}

	OnMessage(Event)
	{
if returned data contains "bla"...
func123()
	}

Code: Select all

func123
{
do stuff
this.send(data)
}
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?

Thanks!
Nightwolf85
Posts: 295
Joined: 05 Feb 2017, 00:03

Re: class / function / send  Topic is solved

Post by Nightwolf85 » 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
Guhl
Posts: 25
Joined: 29 May 2018, 01:12

Re: class / function / send

Post by Guhl » 14 Jun 2018, 10:46

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

Return to “Ask For Help”