Using %Net.WebSocket.Client
Hi Guys,
I'm trying to use %Net.WebSocket.Client to collect data from a sever,
.png)
and part of that I needed to implement Two classes (SX3.Production.HTTP.AdvCredenials & SX3.Production.HTTP.AdvListener) as below for the purpose of Credentials & EventListener properties,
Class SX3.Production.HTTP.AdvCredenials Extends %Net.WebSocket.ICredentials
{
Method GetPassword() As %String
{
q ""
}
Method GetUsername() As %String
{
q ""
}
Method GetSSLConfiguration() As %String
{
q "MySSL"
}
}
Class SX3.Production.HTTP.AdvListener Extends %Net.WebSocket.IEventListener
{
Method OnMessage(pContent As %Stream.Object)
{
S ^data("message")=pContent.Read()
}
}
and here where I'm using the WebSocket Client.
I never used WebSocket Client before neither implementing such classes above for the two properties and I would like to know if below code is the right way to then assign WebSoc.Credentials, WebSoc.EventListener and to make the call of WebSoc.URL ?
ClassMethod GetAdvData(Gateway As %String = "", Token) As %String
{
Quit:Gateway="" 1
Set:Gateway[":" Gateway=$TR(Gateway,": ")
Set Found=0
S BleMac="DC:0D:30:00:07:25",GatewayMac="CC:1B:E0:E2:52:D4" // this for testing
Set WebSoc=##class(%Net.WebSocket.Client).%New()
Set WebSoc.Credentials="SX3.Production.HTTP.AdvCredenials"
Set WebSoc.EventListener="SX3.Production.HTTP.AdvListener"
Set WebSocURL="https://domain3wGT.com/api/gap/nodes?filter_mac="_BleMac_"&filter_rssi=-75&mac="_GatewayMac_"&active=1&event=1&chip=1&access_token="_Token
S WebSoc.URL=WebSocURL
Quit 1
}
Thanks
Comments
About 5 years back, I composed an example for a WebSockel Client
might be a starting point
I actually I've seen that one before posting the question, but couldn't find the actual code, do I have to run those docker commands, I'm not familiar with dockers and I don't want to mess up our Live Linux system, so not looking to try or test it if only can check a copy of the code that would be great?
Thanks Robert
The article https://community.intersystems.com/post/websocket-client-iris-internal
has a link to OEX https://openexchange.intersystems.com/package/IRIS-internal-WebSocket-Client
which points to code on Github that includes a Client and an Echo Server for testing
the code runs on any IRIS. Docker is just a helping platform
Thanks but it looks like you are not using Credential class (CRE.cls)?
I think in my case it should be simpler, only need to create an instance of the WebsocketClient, set the URL and implement Credentials & the EventLister, and I can where you used EventLister but don't know where to use the Credentials class?
But I also noticed that in your Try method you're actually sending message, in my case I'm looking to get messages, but if you can let me know where to use Credentials, that would be a good start for me to try and get data?
Thanks
You are right, The example defines
Class WSCI.CRE Extends (%RegisteredObject, %Net.WebSocket.ICredentials)
but doesn't use it,
According to the class definition of %Net.WebSocket.Client you pass it as a parameter
in the %New() method..png)
so you code (extending my example may look like this:
set cre=##class(WSCI.CRE).%New()
#; feed whatever your credentials need
#; init connection set ws=##class(%Net.WebSocket.Client).%New(url,cre,evl)It might be worth checking what class %Net.WebSocket.Client does with your credentials
Hi Robert,
here is how my code looks like but when I comes to line code : Set WebSoc=##class(%Net.WebSocket.Client).%New(WebSocURL,cre,evl)
it just hangs and doesn't go any further!?
ClassMethod GetAdvData() As %String
{
S Token=##class(SX3.Task.TemperatureCollection).GetAuth()
S BleMac="DC:0D:30:00:07:25",GatewayMac="CC:1B:E0:E2:52:D4" // this for testing
set cre=##class(SX3.Production.HTTP.AdvCredenials).%New()
set evl=##class(SX3.Production.HTTP.AdvListener).%New()
Set WebSocURL="https://ac1.mqtt.sx3ac.com/api/gap/nodes?filter_mac="_BleMac_"&filter_rssi=-75&mac="_GatewayMac_"&active=1&event=1&chip=1&access_token="_Token
Set WebSoc=##class(%Net.WebSocket.Client).%New(WebSocURL,cre,evl)
S res=WebSoc.Next(30,1)
Quit WebSoc_"|"_WebSocURL
}
Class SX3.Production.HTTP.AdvCredenials Extends (%RegisteredObject, %Net.WebSocket.ICredentials)
{
Method GetUsername() As %String
{
quit "SuperUser"
}
/// Returns the password to use for authentication with the web socket
/// @API.Overrideable
Method GetPassword() As %String
{
quit "SYS"
}
Method GetSSLConfiguration() As %String
{
q "RTLS"
}}
Class SX3.Production.HTTP.AdvListener Extends (%RegisteredObject, %Net.WebSocket.IEventListener)
{Method OnMessage(pContent As %Stream.Object)
{
S ^data("message")="1" //pContent.Read()
}
}
Also changed the listener class to include you code and still the same, hangs
Class SX3.Production.HTTP.AdvListener Extends (%RegisteredObject, %Net.WebSocket.IEventListener)
{Method OnOpen()
{
if $increment(%ct("O")) set io=$io use 0 write "OPEN",! break:$get(%b) use io
}/// Called when an error occurs in the web socket connection.<br />
/// @API.Overrideable
Method OnError(pError As %Exception.AbstractException)
{
if $increment(%ct("E")) set io=$io use 0 write !
do $system.OBJ.DisplayError()
if pError.Code=28000 kill ws use io quit ;connection lost
zwrite pError write ! break:$get(%b) use io
}/// Called when the web socket connection is closed by either the client (%OnClose) or the server (by sending a close frame).<br />
/// @API.Overrideable
Method OnClose()
{
if $increment(%ct("C")) set io=$io use 0 write "CLOSE",! BREAK:$get(%b) use io
}/// Called when the client receives a message from the server. <br />
/// <var>pContent</var> may be a binary or UTF8-decoded character stream. <br />
/// @API.Overrideable
Method OnMessage(pContent As %Stream.Object)
{
if $increment(%ct("M")) set io=$io use 0 write "MESSAGE:",pContent,!
if $isobject(pContent) write pContent.Read(),!
if $get(%b) zwrite pContent BREAK
use io
}}
any clues?
Thanks
HMMM.
Are you sure that WS and WSS Protocol are enabled on the Webserver
https://ac1.mqtt.sx3ac.com
And any firewall in between?