Last active
July 2, 2020 09:37
-
-
Save zarlin/835bfc63b13106f712f725d8e4f0379c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GameSparks; | |
using System; | |
/// <summary> | |
/// Custom wrapper around the NativeWebSocket from https://github.com/endel/NativeWebSocket | |
/// Intended to fix the issue of the GameSparks SDK not supporting .net4.x | |
/// </summary> | |
public class CustomGamesparksSocket : IGameSparksWebSocket | |
{ | |
private NativeWebSocket.WebSocket socket = null; | |
private Action<string> onError = null; | |
public void Initialize(string url, Action<string> onMessage, Action onClose, Action onOpen, Action<string> onError) | |
{ | |
socket = new NativeWebSocket.WebSocket(url); | |
socket.OnOpen += () => { onOpen(); }; | |
socket.OnClose += (NativeWebSocket.WebSocketCloseCode code) => { onClose(); }; | |
this.onError = onError; | |
socket.OnError += OnSocketError; | |
socket.OnMessage += (byte[] data) => | |
{ | |
var message = System.Text.Encoding.UTF8.GetString(data); | |
onMessage(message); | |
}; | |
} | |
public void Initialize(string url, Action<byte[]> onMessage, Action onClose, Action onOpen, Action<string> onError) | |
{ | |
socket = new NativeWebSocket.WebSocket(url); | |
socket.OnOpen += () => { onOpen(); }; | |
socket.OnClose += (NativeWebSocket.WebSocketCloseCode code) => { onClose(); }; | |
this.onError = onError; | |
socket.OnError += OnSocketError; | |
socket.OnMessage += (byte[] data) => | |
{ | |
onMessage(data); | |
}; | |
} | |
private void OnSocketError(string message) | |
{ | |
onError(message); | |
} | |
/// <summary> | |
/// This must be called every frame from the Unity main thread (Update) to process received messages | |
/// </summary> | |
public void DispatchMessageQueue() | |
{ | |
socket.DispatchMessageQueue(); | |
} | |
public async void Open() | |
{ | |
await socket.Connect(); | |
} | |
public async void Close() | |
{ | |
await socket.Close(); | |
} | |
public async void Terminate() | |
{ | |
await socket.Close(); | |
} | |
public void Send(string request) | |
{ | |
socket.SendText(request); | |
} | |
public void SendBinary(byte[] request, int offset, int length) | |
{ | |
byte[] payload = new byte[length]; | |
Array.Copy(request, offset, payload, 0, length); | |
socket.Send(payload); | |
} | |
public GameSparksWebSocketState State | |
{ | |
get | |
{ | |
if (socket == null) | |
return GameSparksWebSocketState.None; | |
switch (socket.State) | |
{ | |
case NativeWebSocket.WebSocketState.Connecting: | |
return GameSparksWebSocketState.Connecting; | |
case NativeWebSocket.WebSocketState.Open: | |
return GameSparksWebSocketState.Open; | |
case NativeWebSocket.WebSocketState.Closing: | |
return GameSparksWebSocketState.Closing; | |
case NativeWebSocket.WebSocketState.Closed: | |
return GameSparksWebSocketState.Closed; | |
default: | |
return GameSparksWebSocketState.None; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment