GithubHelp home page GithubHelp logo

shepherd0619 / unitywebsocketutility Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 50 KB

A script serves as template of WebSocket in Unity.

License: BSD 3-Clause "New" or "Revised" License

C# 100.00%
gamedev unity websocket

unitywebsocketutility's Introduction

UnityWebSocketUtility

A script serves as template of WebSocket in Unity.

WebSocketUtility is a utility class written in C# for Unity that provides functionality for establishing and managing WebSocket connections. It allows sending and receiving messages over the WebSocket protocol.

Features

  • Connect to a WebSocket server using a specified URI.
  • Send messages to the server.
  • Receive messages from the server.
  • Handle received JSON data.
  • Implement heartbeat functionality to check the connection status.

Usage

  1. Create an instance of the WebSocketUtility class by providing the WebSocket server URI.
Uri serverUri = new Uri("wss://example.com");
WebSocketUtility wsUtility = new WebSocketUtility(serverUri);
  1. Connect to the WebSocket server.
wsUtility.Connect();
  1. Send messages to the server.
string message = "Hello, server!";
wsUtility.Send(message);
  1. Handle received JSON datab by subscribing to the OnReceiveJson event. registering callback with protocol.
wsUtility.RegisterProtocolCallback("HELLO_WORLD", HelloWorld);

private void HelloWorld(string data)
{
    // Process the received JSON data
}
  1. Disconnect from the WebSocket server.
wsUtility.Disconnect();

Dependencies

  • UnityEngine
  • System.Net.WebSockets
  • System.Text
  • System.Threading
  • SimpleJSON
  • Newtonsoft.Json
  • OverwatchLogger

Tips

  1. You may notice HttpManager (from Biozone.Networking). It's just an empty Monobehaviour whose mission is host coroutines. You can replace it with your own.

unitywebsocketutility's People

Contributors

shepherd0619 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.