I am using cocos2d-x v3.17.2. I create a Network
class to connect with my local server through websocket as below. When I build an android app, I keep getting the error of connection failed
.
var Network = cc.Class.extend({
_ws: null,
ctor: function() {
},
connect: function(serverUrl) {
this.connectToWebSocket(serverUrl);
},
connectToWebSocket: function(serverUrl) {
this._ws = new WebSocket(serverUrl, []);
this._ws.onopen = this.onOpen.bind(this);
this._ws.onmessage = this.onMessage.bind(this);
this._ws.onclose = this.onClose.bind(this);
this._ws.onerror = this.onError.bind(this);
},
onOpen: function(event) {
cc.log("onOpen");
this._ws.send("Hello WebSocket, I'm a text message.");
},
onMessage: function(event) {
console.log('WebSocket message received:', event);
},
onClose: function(event) {
cc.log("onClose: ", JSON.stringify(event));
},
onError: function(event) {
cc.log("onError: ", JSON.stringify(event));
},
onDestroy: function() {
if(this._ws) {
this._ws.close();
}
}
})