https://github.com/magicblock-labs/nativewebsocket
https://github.com/magicblock-labs/nativewebsocket
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/magicblock-labs/nativewebsocket
- Owner: magicblock-labs
- License: mit
- Created: 2023-01-27T12:35:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-19T12:18:30.000Z (over 1 year ago)
- Last Synced: 2025-04-23T06:05:25.712Z (over 1 year ago)
- Language: C#
- Homepage: https://www.nuget.org/packages/native-websocket
- Size: 48.8 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# NativeWebSocket
NativeWebSocket uses a combination of [NativeWebSocket](https://github.com/endel/NativeWebSocket) and [websocket-sharp](https://github.com/sta/websocket-sharp) to work on all the tested Unity build platform (WebGL, IOS, Android, Dektop).
Both packages has been made compatible with Unity, compiling them for dotnet 2.0 and the convience interfaca make the internal implementation transparents to the users.
## How to Install
Drag into Unity the 4 needed files:
- NativeWebSocket.dll
- NativeWebSocket.jslib
- websocket-sharp-latest.dll
- NativeWebSocket.c
The files are available at the latest release: https://github.com/garbles-labs/NativeWebSocket/releases.
### WebGL
If you are compiling for WebGL, add this script to any object, e.g. the main camera.
This ensure that the websockets callback are always executed promtly, on the main thread.
```csharp
public class SetupMainThread : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Setup()
{
MainThreadUtil.Setup();
}
}
```
## How to Use:
### Creating a connection
```csharp
webSocket = NativeWebSocket.WebSocket.Create("wss://api.mainnet-beta.solana.com:443");
```
### Opening the connection, sending and receive messages
```csharp
webSocket.OnOpen += () =>
{
Debug.Log("Connection open!");
string accountSubscribeParams =
"{\"jsonrpc\":\"2.0\",\"id\":" + 0 +
",\"method\":\"accountSubscribe\",\"params\":[\"" + acc.Key +
"\",{\"encoding\":\"jsonParsed\",\"commitment\":\"confirmed\"}]}";
webSocket.Send(System.Text.Encoding.UTF8.GetBytes(accountSubscribeParams)).RunSynchronously();
};
webSocket.OnMessage += bytes =>
{
var message = System.Text.Encoding.UTF8.GetString(bytes);
Debug.Log("SocketMessage:" + message);
};
webSocket.OnClose += (e) => Debug.Log("Connection closed!");
webSocket.Connect();
```