public static async Task Start(Session session, CancellationToken cancellationToken) { await Task.Delay(30000);//delay running 30s System.Net.ServicePointManager.Expect100Continue = false; cancellationToken.ThrowIfCancellationRequested(); var socketURL = session.LogicSettings.DataSharingDataUrl; using (var ws = new WebSocketSharp.WebSocket(socketURL)) { ws.Log.Level = WebSocketSharp.LogLevel.Error; //ws.OnMessage += (sender, e) => // Console.WriteLine("New message from controller: " + e.Data); while (true) { try { ws.Connect(); Logger.Write("Pokemon spawn point data service connection established."); while (ws.ReadyState == WebSocketSharp.WebSocketState.Open) { lock (events) { while (events.Count > 0) { if (ws.ReadyState == WebSocketSharp.WebSocketState.Open) { var item = events.Dequeue(); var data = Serialize(item); ws.Send($"42[\"pokemon\",{data}]"); } } } await Task.Delay(POLLING_INTERVAL); ws.Ping(); } } catch (IOException) { session.EventDispatcher.Send(new ErrorEvent { Message = "The connection to the data sharing location server was lost." }); } catch (Exception) { } finally { //everytime disconnected with server bot wil reconnect after 15 sec await Task.Delay(POLLING_INTERVAL, cancellationToken); } } } }
private static void SendMeshToSP(MeshInfo meshInfo) { if (!webSocket.IsAlive) { ConnectSocket(); } if (!webSocket.IsAlive || !webSocket.Ping()) { EditorUtility.DisplayDialog( "Send to Substance Painter", "Substance Painter is not detected.\n" + "Please check if Substance Painter is correctly started and if the \"unity-link\" plugin is enabled.", "OK"); return; } // Ensure compatibility foreach (Material material in meshInfo.Materials) { if (ShadersInfos.ContainsShader(material.shader)) { ShadersInfos.GetShaderInfos(material.shader).EnsureMaterialCompatibility(material); } } // Check if a project already exist string substancePainterProjectPath = GetSpProjectPath(meshInfo); if (File.Exists(substancePainterProjectPath)) { // If the project exists, open it then reimport the mesh Debug.Log(string.Format("Reopening Substance Painter project located at {0}", substancePainterProjectPath)); OpenSpProject(meshInfo, substancePainterProjectPath); } else { // If the project doesn't exist, create a new one and save it here Debug.Log(string.Format("Creating a new Substance Painter project located at {0}", substancePainterProjectPath)); CreateSpProject(meshInfo, substancePainterProjectPath); } }
public static async Task Start(Session session, CancellationToken cancellationToken) { await Task.Delay(30000, cancellationToken);//delay running 30s System.Net.ServicePointManager.Expect100Continue = false; cancellationToken.ThrowIfCancellationRequested(); var socketURL = session.LogicSettings.DataSharingDataUrl; using (var ws = new WebSocketSharp.WebSocket(socketURL)) { ws.Log.Level = WebSocketSharp.LogLevel.Fatal; ws.Log.Output = (logData, message) => { //silenly, no log exception message to screen that scare people :) }; ws.OnMessage += (sender, e) => { onSocketMessageRecieved(session, sender, e); }; ws.Connect(); while (true) { cancellationToken.ThrowIfCancellationRequested(); try { if (retries == 5) //failed to make connection to server times contiuing, temporary stop for 10 mins. { session.EventDispatcher.Send(new WarnEvent() { Message = "Couldn't establish the connection to necro socket server, Bot will re-connect after 10 mins" }); await Task.Delay(1 * 60 * 1000, cancellationToken); retries = 0; } if (events.Count > 0 && ws.ReadyState != WebSocketSharp.WebSocketState.Open) { retries++; ws.Connect(); } while (ws.ReadyState == WebSocketSharp.WebSocketState.Open) { //Logger.Write("Connected to necrobot data service."); retries = 0; lock (events) { processing.Clear(); processing.AddRange(events); } if (processing.Count > 0 && ws.IsAlive) { if (processing.Count == 1) { //serialize list will make data bigger, code ugly but save bandwidth and help socket process faster var data = Serialize(processing.First()); ws.Send($"42[\"pokemon\",{data}]"); } else { var data = Serialize(processing); ws.Send($"42[\"pokemons\",{data}]"); } } lock (events) { events.RemoveAll(x => processing.Any(t => t.EncounterId == x.EncounterId)); } await Task.Delay(POLLING_INTERVAL, cancellationToken); ws.Ping(); } } catch (IOException) { session.EventDispatcher.Send(new WarnEvent { Message = "Disconnect to necro socket. New connection will be established when service available..." }); } catch (Exception) { } finally { //everytime disconnected with server bot wil reconnect after 15 sec await Task.Delay(POLLING_INTERVAL, cancellationToken); } } } }
public static async Task Start(Session session, CancellationToken cancellationToken) { await Task.Delay(30000);//delay running 30s System.Net.ServicePointManager.Expect100Continue = false; cancellationToken.ThrowIfCancellationRequested(); var socketURL = session.LogicSettings.DataSharingDataUrl; using (var ws = new WebSocketSharp.WebSocket(socketURL)) { ws.Log.Level = WebSocketSharp.LogLevel.Fatal; ws.Log.Output = (logData, message) => { //silenly, no log exception message to screen that scare people :) }; //ws.OnMessage += (sender, e) => // Console.WriteLine("New message from controller: " + e.Data); while (true) { try { if (retries++ == 5) //failed to make connection to server times contiuing, temporary stop for 10 mins. { session.EventDispatcher.Send(new WarnEvent() { Message = "Couldn't establish the connection to necro socket server, Bot will re-connect after 10 mins" }); await Task.Delay(10 * 1000 * 60); } ws.Connect(); if (ws.ReadyState == WebSocketSharp.WebSocketState.Open) { Logger.Write("Pokemon spawn point data service connection established."); retries = 0; while (ws.IsAlive) { lock (events) { while (events.Count > 0) { processing.Add(events.Dequeue()); } } while (processing.Count > 0) { if (ws.IsAlive) { var item = processing.FirstOrDefault(); var data = Serialize(item); ws.Send($"42[\"pokemon\",{data}]"); processing.Remove(item); await Task.Delay(processing.Count > 0? 3000 : 0); } } } await Task.Delay(POLLING_INTERVAL); ws.Ping(); } } catch (IOException) { session.EventDispatcher.Send(new WarnEvent { Message = "Disconnect to necro socket. New connection will be established when service available..." }); } catch (Exception) { } finally { //everytime disconnected with server bot wil reconnect after 15 sec await Task.Delay(POLLING_INTERVAL, cancellationToken); } } } }