123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import { _decorator, Component, Node, Game, sys } from 'cc';
- import { Constant } from '../constant';
- import { GameMng } from '../GameMng';
- import GameUtil from '../gcommon/GameUtil';
- import { Tools } from '../Tools';
- import { msgManager } from './msgManager';
- const { ccclass, property } = _decorator;
- @ccclass('gameSocket')
- export default class gameSocket {
- private static _instance: gameSocket | null = null;
- public static get Instance() {
- if (gameSocket._instance === null)
- gameSocket._instance = new gameSocket();
- return gameSocket._instance;
- }
- sock:WebSocket = null;
- is_connected:Boolean = false;
- stopConnected:Boolean = false;
- check_heartbeat_succeed:boolean = false;
- is_online_status:boolean = false; //是否是在线状态
- _on_opened(event:Event):void
- {
- if(this.is_connected===true){
- }else{
- this.is_connected = true;
- this.check_heartbeat_succeed = true;
- gameSocket.Instance.is_online_status = true;
- if(GameMng._userData!=null){
- msgManager.relink()
- }else{
-
- }
- }
- console.log("_on_opened",event)
- }
- _on_recv_data(event:any):void
- {
- this.check_heartbeat_succeed = true;
- if(event.data===Constant.RECV_HEART_BEAT){
- //console.log("接受服务端返回的心跳",GameUtil.curTime())
- return
- }
- if(event.data===Constant.RECV_CHEAK_HEART_BEAT){
- gameSocket.Instance.heartbeat();
- return
- }
- if(event.data===""||event.data===null){
- console.assert(true,"接受输出格式错误,data为空",event)
- }else{
- msgManager.recv(event.data)
- }
- }
- _on_socket_close(event:any):void
- {
- this.is_connected = false;
- console.log("_on_socket_close",event)
- }
- _on_socket_err(event:any):void
- {
- this.is_connected = false;
- // this.close()
- console.log("_on_socket_err",event)
- }
- connect(url:any):void
- {
- if(this.stopConnected){
- console.log("客户端退出链接,暂不提供重连")
- return
- }
- if(!this.is_connected){
- try{
- this.sock = new WebSocket(url);
- this.sock.binaryType = "arraybuffer"; //"arraybuffer", "blob", "document", "json", and "text".
- this.sock.onopen = this._on_opened.bind(this);
- this.sock.onmessage = this._on_recv_data.bind(this);
- this.sock.onclose = this._on_socket_close.bind(this);
- this.sock.onerror = this._on_socket_err.bind(this);
- }catch(e:any){
- console.log("connect error",e)
- }
-
- }
-
- }
- heartbeat(){
- this.check_heartbeat_succeed = false;
- this.send_cmd(0);
- }
- // this.sock.readyState
- // CONNECTING:值为0,表示正在连接;
- // OPEN:值为1,表示连接成功,可以通信了;
- // CLOSING:值为2,表示连接正在关闭;
- // CLOSED:值为3,表示连接已经关闭,或者打开连接失败。
- send_cmd(body):void
- {
- if (!this.sock || !this.is_connected) {
- return;
- }
- console.log("发送消息",body)
- try{
- if( this.sock.readyState===1){
- if( typeof body == 'number'){
- body=body+""
- }
- this.sock.send(body);
- }
- // if(sys.isBrowser){
-
- // }else{
- // this.sock.send(body);
- // }
-
-
- }catch(e:any){
- console.log("e",e)
- }
-
-
- }
- close():void
- {
- if (this.sock !== null) {
- this.sock.close();
- this.sock = null;
- }
- this.is_connected = false;
- this.check_heartbeat_succeed = false;
- }
- }
|