GithubHelp home page GithubHelp logo

konoplyanikovd-zz / nativewebsocket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from endel/nativewebsocket

0.0 0.0 0.0 103 KB

๐Ÿ”Œ WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, iOS, UWP)

License: Other

C# 96.30% JavaScript 3.70%

nativewebsocket's Introduction

Native WebSocket

This is the simplest and easiest WebSocket library for Unity you can find!

  • No external DLL's required (uses built-in System.Net.WebSockets)
  • WebGL/HTML5 support
  • Supports all major build targets
  • Very simple API

This WebSocket client is used on colyseus-unity3d.
Consider supporting my work on Patreon.
Support me on Patreon

Installation

Requires Unity 2019.1+ with .NET 4.x+ Runtime

Install via UPM (Unity Package Manager)

  1. Open Unity
  2. Open Package Manager Window
  3. Click Add Package From Git URL
  4. Enter URL: https://github.com/endel/NativeWebSocket.git#upm

Install manually

  1. Download this project
  2. Copy the sources from NativeWebSocket/Assets/WebSocket into your Assets directory.

Usage

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using NativeWebSocket;

public class Connection : MonoBehaviour
{
  WebSocket websocket;

  // Start is called before the first frame update
  async void Start()
  {
    websocket = new WebSocket("ws://localhost:2567");

    websocket.OnOpen += () =>
    {
      Debug.Log("Connection open!");
    };

    websocket.OnError += (e) =>
    {
      Debug.Log("Error! " + e);
    };

    websocket.OnClose += (e) =>
    {
      Debug.Log("Connection closed!");
    };

    websocket.OnMessage += (bytes) =>
    {
      Debug.Log("OnMessage!");
      Debug.Log(bytes);

      // getting the message as a string
      // var message = System.Text.Encoding.UTF8.GetString(bytes);
      // Debug.Log("OnMessage! " + message);
    };

    // Keep sending messages at every 0.3s
    InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);

    // waiting for messages
    await websocket.Connect();
  }

  void Update()
  {
    #if !UNITY_WEBGL || UNITY_EDITOR
      websocket.DispatchMessageQueue();
    #endif
  }

  async void SendWebSocketMessage()
  {
    if (websocket.State == WebSocketState.Open)
    {
      // Sending bytes
      await websocket.Send(new byte[] { 10, 20, 30 });

      // Sending plain text
      await websocket.SendText("plain text message");
    }
  }

  private async void OnApplicationQuit()
  {
    await websocket.Close();
  }

}

Demonstration

1. Start the local WebSocket server:

cd Server
npm install
npm start

2. Open the NativeWebSocket/Assets/WebSocketExample/WebSocketExampleScene.unity on Unity and Run.

Acknowledgements

Big thanks to Jiri Hybek. This implementation is based on his work.

License

Apache 2.0

nativewebsocket's People

Contributors

endel avatar codingben avatar mentaman 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.