Websocket events do not access webmethods

Answered
0
0

Hi,

I am trying to access some devices on client-side with Websockets. The devices are accessed via an application installed on client side and there is a websocket server waiting for requests. Everything is working on html/JS to call websocket, but it does not work on Wisej.

Below is the JS integrated on Wisej and the code on wisej form,

//Javascript code

var wsUri = “ws://localhost:8888/”,
ws = new WebSocket(wsUri);

ws.onopen = function (e) {
console.log(“WebSocket OPEN: “);
this.WriteToScreen(“CONNECTED”);};

ws.onclose = function (e) {
this.WriteToScreen(“DISCONNECTED”);};

ws.onmessage = function (e) {
console.log(“RESPONSE: ” + e.data);
this.WriteToScreen(“RESPONSE: ” + e.data);};

ws.onerror = function (e) {
this.WriteToScreen(“ERROR: ” + e.data);};

this.Send = function(message) {
this.WriteToScreen(“SENT: ” + message);
this.websocket.send(message);}

//Wisej form code

<WebMethod>
Public Sub WriteToScreen(v_message As String)
txt_message.Text = v_message
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Call(“Send”, txt_message.Text)
End Sub
End Class

When we call JS method via Me.Call(“Send”, txt_message.Text), the <WebMethod> WriteToScreen is called back without a problem but when an event fires like ws.onopen I receives the message “Uncaught TypeError: this.WriteToScreen is not a function at WebSocket.ws.onopen …”

What can I do to solve this problem?

Regards

 

 

 

 

  • You must to post comments
Best Answer
0
0

This is not a Wisej issue. It has nothing to do with the way javascript or websocket work.

The error in your case is that you are using “this” in the wrong context.

  • You must to post comments
0
0

Hi Luca,

I solved using

var me = this;

and on events I call the webmethod like me.WriteToScreen(“CONNECTED”);

 

Thank you for your quick response.

Regards

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.