Giter Club home page Giter Club logo

websocketplayer's Introduction

WebsocketPlayer

websocketplayer_controllergun

WebsocketPlayer is a Unity plugin designed to expose cameras and internal C# functions via both HTTP and Websockets, perfect for the creation of web based local Unity client(s).

Features

  • Subsecond video streaming
  • Multiple cameras supported
  • Per camera quality, resolution and FPS controls
  • Realtime camera FPS counter
  • Extendable HTTP router class
  • HTML demo with router example
  • Windows, Mac, Linux support (Mono)

Install

Dependencies

WebsocketPlayer requires the System.Drawing library, but Unity by default does not support it.

%UnityFolder%\Editor\Data\Mono\lib\mono\2.0

You can manually copy the library to Assets/Packages from the above path so your project can reference it.

Windows Specific

You can use the included NuGet package config or the following command.

Install-Package WebSocketSharp-netstandard -Version 1.0.1

Remove the .NET versions that you do not wish to target, check your Unity Mono Api Compatibility Level in PlayerSettings/Configuration.

Usage

Quick Start

Simply import the latest version from releases and place the WebsocketPlayerController prefrab into your project.

Add the WebsocketCamera to any camera object that you wish to be accessible via websocket.

Press play.

WebsocketPlayerController Prefab

HTTP Root

By default, the WebPlayerTemplates directory is used as the web server root. This Unity designated folder ignores HTML/JS specifically for web use.

Router.cs

Router.cs derives in the Example Scene included in the plugin. For future updates it is best to not directly edit this, but rather recreate the router outside of the example.

Functions and inline code executed by a web route will exist in the web server thread. The Unity API is not thread-safe, thus API calls will display in the console as an error. There are multiple solutions, such as using instances defined from Start(), dispatched IEnumerator functions and using nonserialized variables.

using System.Text;
using WebSocketSharp;
using WebSocketSharp.Net;
using Jamestel.WebsocketPlayer.WebServer;

public class Routes 
{
    Router.WebRoutes.Add(new WebRoute(
        "GET",                // Type of request, GET or POST supported
        "/test/helloworld",   // Path to the function ex: http://localhost/test/helloworld
        FunctionToExecute     // Function to be executed, lambda syntax also supported
    ));
    
    // WebSocketSharp listener & request are expected arguments
    void FunctionToExecute(HttpListenerRequest request, HttpListenerResponse response)
    {
        string content = "Hello World";

        // Define content type as HTML and encoding UTF-8
        response.ContentType = "text/html";
        response.ContentEncoding = Encoding.UTF8;

        // Write response, get bytes of string for final byte array
        response.WriteContent(Encoding.UTF8.GetBytes(content));
    }
}

Feature Roadmap

  • TurboJPEG integration
  • Wildcard support for web routes
  • Camera texture performance
  • Bidirectional websocket communication
  • Better error handling
  • Websocket audio?

websocketplayer's People

Contributors

jamestel avatar

Stargazers

 avatar

Watchers

 avatar  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.