I have a game written with cocos creator 2.4.5, if built for mobile web, the game can work normally. but when building android, the above game does not work (cannot connect socket)
Many bugs can have here, upload your debug image please. Maybe because connection string need includes wss:
Please upload your WebSocket connection related code so that we can assess whether there are any vulnerabilities.
Below is my code, I think there is a problem when exporting android, I only get an error after exporting android, if building mobile web it works perfectly. cocos cretor 2.4.5
const startTimeout = () => {
if (timeoutRef) {
clearTimeout(timeoutRef);
}
timeoutRef = setTimeout(() => {
console.warn('No message received for 30 seconds, returning to lobby.');
this.actBack(); // Call the function to return to lobby.
}, timeoutDuration);
};
// Initially, set the timeout.
startTimeout();
this.acPlayer.node.active = false;
this.acBanker.node.active = false;
this.acTIE.node.active = false;
this.acPlayerPair.node.active = false;
this.acBankerPair.node.active = false;
this.socket = new WebSocket('ws://kubet1.sin79.com');
console.log('Trying to establish WebSocket connection...');
this.socket.binaryType = "arraybuffer"; // Đặt kiểu dữ liệu nhận được là "arraybuffer"
this.socket.onopen = () => {
console.log('WebSocket connection established.');
// Đăng ký với server bằng tên người dùng hoặc ID.
this.socket.send(JSON.stringify({
event: 'register',
data: {
nick_name: Configs.Login.Nickname
}
}));
console.log('Sent registration data to the server.');
};
I tested it and it works normally.
const {ccclass, property} = cc._decorator;
@ccclass
export default class Helloworld extends cc.Component {
wssLink: string = "ws://192.168.52.189:8002";
start () {
// init logic
this.label.string = this.text;
this.connectToWsserver();
}
connectToWsserver (cb?: Function){
var _wsiSendBinary = new WebSocket(this.wssLink, []);
_wsiSendBinary.onopen = function () {
console.log("[Connection Server Succes] with CocosCreator 2.4.5");
};
}
}