| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- window.lockStepClient = {
- matchCallback:null,
- onRecvFromClientCallback:null,
- onPlayerQuitCallback:null,
- login(openid,name,avatarUrl,gender,callback)
- {
- if(openid ==='' || name ==='' ||avatarUrl ==='' ||gender ==='' ||callback == null) return;
- let Self = this;
-
- // this.ws = new WebSocket("ws://localhost:3000");
- // this.ws = new WebSocket("ws://121.4.59.141:3000/node/");
- // this.ws = new WebSocket("ws://www.yuyekeji.cn:3000/node/");
- this.ws = new WebSocket("ws://www.yuyekeji.cn:3000/");
- // this.ws = new WebSocket("ws://192.168.31.211:3000");
-
- this.ws.onopen = function () {
- //当WebSocket创建成功时,触发onopen事件
- console.log("WebSocket建立成功");
- console.log("open");
- let player_info = {};
- player_info.type = "login";
- player_info.openid = openid;
- player_info.name = name;
- player_info.avatarUrl = avatarUrl;
- player_info.gender = gender;
- // console.log('player_info=',player_info)
- Self.ws.send(JSON.stringify(player_info)); //将消息发送到服务端
- callback();
- }
- this.ws.onmessage = function (e) {
- //当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
- // console.log(e.data);
- let data_json = JSON.parse(e.data);
- if(data_json.type === 'match')
- {
- Self.matchCallback(data_json.openid,data_json.name,data_json.avatarUrl,data_json.gender);
- }
- else if(data_json.type === 'message')
- {
- Self.onRecvFromClientCallback(data_json.message);
- }
- }
- this.ws.onclose = function (e) {
- //当客户端收到服务端发送的关闭连接请求时,触发onclose事件
- console.log("close");
- }
- this.ws.onerror = function (e) {
- //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
- console.log(error);
- }
- },
- match(openid,callback)
- {
- if(openid ==='' || callback == null) return;
- let obj = {};
- obj.type = 'match';
- obj.openid = openid;
- this.matchCallback = callback;
- this.ws.send(JSON.stringify(obj));
- },
- sendMessage(openid,message)
- {
- if(openid ==='' || message ==='') return;
- let obj = {};
- obj.type = 'message';
- obj.openid = openid;
- obj.message = message;
- this.ws.send(JSON.stringify(obj));
- },
- onRecvFromClient(callback){
- this.onRecvFromClientCallback = callback;
- },
- cancel()
- {
- this.ws.close();
- },
- onOtherPlayerQuit(openid,callback)
- {
- if(openid ==='') return;
- let obj = {};
- obj.type = 'onQuit';
- obj.openid = openid;
- callback = this.onPlayerQuitCallback;
- }
- }
- module.exports = lockStepClient;
|