public static ServervilleWSComponent Get() { GameObject obj = GameObject.Find("/Serverville"); if (obj == null) { obj = new GameObject("Serverville"); DontDestroyOnLoad(obj); } ServervilleWSComponent ws = obj.GetComponent <ServervilleWSComponent>(); if (ws == null) { ws = obj.AddComponent <ServervilleWSComponent>(); } return(ws); }
public void Init(OnErrorReply onConnected) { GetSerializerSettings(); ServervilleWSComponent wsComp = ServervilleWSComponent.Get(); wsComp.UpdateEvent += Update; string url = SV.ServerURL + "/websocket"; ServerSocket = new WebSocket(url); ReplyCallbacks = new Dictionary <string, MessageReplyClosure>(); ServerSocket.OnOpen += (object sender, EventArgs e) => { if (onConnected != null) { onConnected(null); } }; ServerSocket.OnClose += OnWSClosed; ServerSocket.OnMessage += OnWSMessage; ServerSocket.OnError += (object sender, ErrorEventArgs e) => { Debug.Log("Connection error: " + e.ToString()); if (onConnected != null) { onConnected(ErrorReply.makeClientErrorCode(-2, e.Message)); } }; ServerSocket.Connect(); }