Javascript call method in another method
Now I am trying to implement Unity Webgl using jslib. I am so confused about how to call a method in another method function. I want to call the Recv method when a message (ws.onmessage) appears. But it shows "TypeError: this.Recv is undefined". Could you please help me to understand this source?
Thank!!!!!
Here's my source code
var ws = null;
var init_url = "";
var received_msg = "";
var error_msg = "";
var WebsocketLib = {
Hello: function(){
window.alert("Hello,world!");
},
InitSocket: function(url){
init_url = Pointer_stringify(url);
console.log("InitWebSocket: "+init_url);
ws = new WebSocket(init_url);
ws.onopen = function(evt){
console.log("Connect");
isConnected = false;
ws.send("hello");
};
ws.onclose = function(evt) {
console.log("Close");
isConnected = false;
};
ws.onmessage = function(evt) {
received_msg = evt.data;
console.log("[recv] "+received_msg);
this.Recv.call(this);
};
ws.onerror = function(evt) {
error_msg = evt.data;
console.log("[error] "+error_msg);
this.Error.call(this);
};
},
Recv: function(){
console.log("[recv] "+received_msg);
var buffer = _malloc(received_msg.length + 1);
writeStringToMemory(returnStr, buffer);
return buffer;
},
Error: function(){
console.log("[error] "+error_msg);
var buffer = _malloc(error_msg.length + 1);
writeStringToMemory(error_msg, buffer);
return buffer;
}
}
source to share
Internally, ws.onmessage this
will refer to ws
(since we are inside the ws method), not WebsocketLib
.
However, inside the Initsocket, where you define the handlers, it this
will correctly (in the sense that this is what you want) refer to the WebsocketLib object, so you can create a bound function to bind the external this
value to be used both this
inside the event handler, for example:
ws.onmessage = function(evt) {
received_msg = evt.data;
console.log("[recv] "+received_msg);
this.Recv.call(this);
}.bind(this);
source to share
meaning this
behaves differently in JavaScript than in other languages. Its meaning depends on how the function is called. Read more about this on the Mozilla MDN page .
To solve your specific problem, you can:
InitSocket: function(url){
var that = this; // [1]
init_url = Pointer_stringify(url);
console.log("InitWebSocket: "+init_url);
ws = new WebSocket(init_url);
ws.onopen = function(evt){
console.log("Connect");
isConnected = false;
ws.send("hello");
};
ws.onclose = function(evt) {
console.log("Close");
isConnected = false;
};
ws.onmessage = function(evt) {
received_msg = evt.data;
console.log("[recv] "+received_msg);
that.Recv.call(that); // [2]
};
ws.onerror = function(evt) {
error_msg = evt.data;
console.log("[error] "+error_msg);
that.Error.call(that); // [2]
};
},
On line 1, I bind the variable this
to a custom variable that I that
choose to call (but you can name it whatever you want). Then on line 2, I used that
instead this
.
Inside the function, the ws.onmessage
value is this
not instance specific WebsocketLib
, so you need to use this "trick" and access the correct value this
using the value stored in the closure inside the value that
.
source to share