Created
July 3, 2020 07:22
-
-
Save dylanh724/4f0d2eb278c79bc1a9f5f8a9b4e353e0 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; | |
using WebSocketSharp; | |
/// <summary> | |
/// Custom wrapper around the WebSocketSharp from https://github.com/sta/websocket-sharp | |
/// Intended to fix the issue of the GameSparks SDK not supporting .net4.x | |
/// https://github.com/Imperium42/gs-csharp-community-sdk/issues/4 | |
/// ------------------------------------------------------------------- | |
/// IMPORTANT: DOWNLOAD DLL @ https://discordapp.com/channels/356100134565838848/548131641491587093/728510612547567646 | |
/// -> Then place it in any /Plugins dir within your Unity /Assets/ | |
/// ------------------------------------------------------------------- | |
/// TODO: Resolve 10 errors. --Xb | |
/// ------------------------------------------------------------------- | |
/// * Templated from Zarlin @ https://gist.github.com/zarlin/835bfc63b13106f712f725d8e4f0379c | |
/// * Edited to go from custom Native WebSocket (lacking Win7 support) to WebSocketSharp | |
/// </summary> | |
public class CustomGamesparksSocket : IGameSparksWebSocket | |
{ | |
private WebSocketSharp.WebSocket socket = null; | |
private Action<string> onError = null; | |
// ........................................................................................................................ | |
private void initAllExceptMsg(string url, Action _onClose, Action _onOpen, Action<string> _onError) | |
{ | |
socket = new WebSocket(url); | |
socket.OnOpen += (sender, e) => _onOpen(); | |
socket.OnClose += (sender, e) => _onClose(); | |
this.onError = _onError; | |
socket.OnError += (sender, e) => OnSocketError(e.Message); | |
} | |
// ........................................................................................................................ | |
/// <summary> | |
/// onMessage:str | |
/// </summary> | |
/// <param name="url"></param> | |
/// <param name="_onMessage"></param> | |
/// <param name="_onClose"></param> | |
/// <param name="_onOpen"></param> | |
/// <param name="_onError"></param> | |
public void Initialize(string url, Action<string> _onMessage, Action _onClose, Action _onOpen, Action<string> _onError) | |
{ | |
initAllExceptMsg(url, _onClose, _onOpen, _onError); | |
socket.OnMessage += (sender, e) => | |
{ | |
string message = e.Data; | |
_onMessage(message); | |
}; | |
} | |
// ........................................................................................................................ | |
/// <summary> | |
/// onMessage:byte[] | |
/// </summary> | |
/// <param name="url"></param> | |
/// <param name="_onMessage"></param> | |
/// <param name="_onClose"></param> | |
/// <param name="_onOpen"></param> | |
/// <param name="_onError"></param> | |
public void Initialize(string url, Action<byte[]> _onMessage, Action _onClose, Action _onOpen, Action<string> _onError) | |
{ | |
initAllExceptMsg(url, _onClose, _onOpen, _onError); | |
socket.OnMessage += (sender, e) => | |
{ | |
//var message = System.Text.Encoding.UTF8.GetString(data); | |
byte[] message = e.RawData; | |
_onMessage(message); | |
}; | |
} | |
// ........................................................................................................................ | |
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.ConnectAsync(); | |
} | |
// ........................................................................................................................ | |
public async void Close() | |
{ | |
await socket.CloseAsync(); | |
} | |
// ........................................................................................................................ | |
public async void Terminate() | |
{ | |
await socket.CloseAsync(); | |
} | |
// ........................................................................................................................ | |
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 WebSocket.WebSocketState.Connecting: | |
return GameSparksWebSocketState.Connecting; | |
case WebSocket.WebSocketState.Open: | |
return GameSparksWebSocketState.Open; | |
case WebSocket.WebSocketState.Closing: | |
return GameSparksWebSocketState.Closing; | |
case WebSocket.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