|
@@ -0,0 +1,73 @@
|
|
|
|
|
+let lockStepClient = {
|
|
|
|
|
+ matchCallback:null,
|
|
|
|
|
+ onMessageRecCallback: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");
|
|
|
|
|
+ // var ws = new WebSocket("ws://121.4.59.141:3000/node/");
|
|
|
|
|
+ // var ws = new WebSocket("ws://www.yuyekeji.cn:3000/node/");
|
|
|
|
|
+ console.log("WebSocket建立成功");
|
|
|
|
|
+
|
|
|
|
|
+ this.ws.onopen = function () {
|
|
|
|
|
+ //当WebSocket创建成功时,触发onopen事件
|
|
|
|
|
+ console.log("open");
|
|
|
|
|
+
|
|
|
|
|
+ let player_info = {};
|
|
|
|
|
+ player_info.type = "login";
|
|
|
|
|
+ player_info.openid = name;
|
|
|
|
|
+ player_info.name = name;
|
|
|
|
|
+ player_info.avatarUrl = avatarUrl;
|
|
|
|
|
+ player_info.gender = gender;
|
|
|
|
|
+ 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.onMessageRecCallback(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));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// module.exports = lockStepClient;
|