Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

KLab/websocket-unitymobile

 
 

Repository files navigation

Logo

Welcome to websocket-sharp!

websocket-sharp supports the followings:

Branches

Build

websocket-sharp is built as a single assembly, websocket-sharp.dll.

websocket-sharp is developed with MonoDevelop. So the simple way to build is to open websocket-sharp.sln and run build for the websocket-sharp project with any of the build configurations (e.g. Debug) in the MonoDevelop.

Install

Self Build

You should add websocket-sharp.dll (e.g. /path/to/websocket-sharp/bin/Debug/websocket-sharp.dll) built yourself to the library references of your project.

If you would like to use that websocket-sharp.dll in your Unity project, you should add that dll to any folder of your project (e.g. Assets/Plugins) in the Unity Editor.

NuGet Gallery

websocket-sharp is available on the NuGet Gallery, as still a prerelease version.

You can add websocket-sharp to your project using the NuGet Package Manager, the following command in the Package Manager Console.

PM> Install-Package WebSocketSharp -Pre

Unity Asset Store

websocket-sharp is available on the Unity Asset Store.

It's priced at US$15. I think your $15 makes this project more better and accelerated, Thank you!

Usage

WebSocket Client

using System;
using WebSocketSharp;

namespace Example
{
  public class Program
  {
    public static void Main (string [] args)
    {
      using (var ws = new WebSocket ("ws://dragonsnest.far/Laputa")) {
        ws.OnMessage += (sender, e) =>
          Console.WriteLine ("Laputa says: " + e.Data);

        ws.Connect ();
        ws.Send ("BALUS");
        Console.ReadKey (true);
      }
    }
  }
}

Step 1

Required namespace.

using WebSocketSharp;

The WebSocket class exists in the WebSocketSharp namespace.

Step 2

Creating an instance of the WebSocket class with the WebSocket URL to connect.

using (var ws = new WebSocket ("ws://example.com")) {
  ...
}

The WebSocket class inherits the System.IDisposable interface, so you can use the using statement.

Step 3

Setting the WebSocket events.

WebSocket.OnOpen Event

A WebSocket.OnOpen event occurs when the WebSocket connection has been established.

ws.OnOpen += (sender, e) => {
  ...
};

e has passed as the System.EventArgs.Empty, so you don't use e.

WebSocket.OnMessage Event

A WebSocket.OnMessage event occurs when the WebSocket receives a WebSocket message.

ws.OnMessage += (sender, e) => {
  ...
};

e has passed as a WebSocketSharp.MessageEventArgs.

e.Type property returns either WebSocketSharp.Opcode.TEXT or WebSocketSharp.Opcode.BINARY that represents the type of the received message. So by checking it, you determine which item you should use.

If e.Type is Opcode.TEXT, you should use e.Data property (returns a string) that represents the received Text message.

Or if e.Type is Opcode.BINARY, you should use e.RawData property (returns a byte []) that represents the received Binary message.

if (e.Type == Opcode.TEXT) {
  // Do something with e.Data
  return;
}

if (e.Type == Opcode.BINARY) {
  // Do something with e.RawData
  return;
}
WebSocket.OnError Event

A WebSocket.OnError event occurs when the WebSocket gets an error.

ws.OnError += (sender, e) => {
  ...
};

e has passed as a WebSocketSharp.ErrorEventArgs.

e.Message property returns a string that represents the error message. So you should use it to get the error message.

WebSocket.OnClose Event

A WebSocket.OnClose event occurs when the WebSocket connection has been closed.

ws.OnClose += (sender, e) => {
  ...
};

e has passed as a WebSocketSharp.CloseEventArgs.

e.Code property returns a ushort that represents the status code that indicates the reason for closure, and e.Reason property returns a string that represents the reason for closure. So you should use them to get the reason for closure.

Step 4

Connecting to the WebSocket server.

ws.Connect ();

If you would like to connect to the server asynchronously, you should use the WebSocket.ConnectAsync () method.

Step 5

Sending a data to the WebSocket server.

ws.Send (data);

The WebSocket.Send method is overloaded.

You can use the WebSocket.Send (string), WebSocket.Send (byte []), and WebSocket.Send (System.IO.FileInfo) methods to send a data.

If you would like to send a data asynchronously, you should use the WebSocket.SendAsync method.

ws.SendAsync (data, completed);

And if you would like to do something when the send is complete, you should set completed to any Action<bool>.

Step 6

Closing the WebSocket connection.

ws.Close (code, reason);

If you would like to close the connection explicitly, you should use the WebSocket.Close method.

The WebSocket.Close method is overloaded.

You can use the WebSocket.Close (), WebSocket.Close (ushort), WebSocket.Close (WebSocketSharp.CloseStatusCode), WebSocket.Close (ushort, string), or WebSocket.Close (WebSocketSharp.CloseStatusCode, string) method to close the connection.

If you would like to close the connection asynchronously, you should use the WebSocket.CloseAsync method.

WebSocket Server

using System;
using WebSocketSharp;
using WebSocketSharp.Server;

namespace Example
{
  public class Laputa : WebSocketService
  {
    protected override void OnMessage (MessageEventArgs e)
    {
      var msg = e.Data == "BALUS"
              ? "I've been balused already..."
              : "I'm not available now.";

      Send (msg);
    }
  }

  public class Program
  {
    public static void Main (string [] args)
    {
      var wssv = new WebSocketServer ("ws://dragonsnest.far");
      wssv.AddWebSocketService<Laputa> ("/Laputa");
      wssv.Start ();
      Console.ReadKey (true);
      wssv.Stop ();
    }
  }
}

Step 1

Required namespace.

using WebSocketSharp.Server;

The WebSocketServer and WebSocketService classes exist in the WebSocketSharp.Server namespace.

Step 2

Creating the class that inherits the WebSocketService class.

For example, if you would like to provide an echo service,

using System;
using WebSocketSharp;
using WebSocketSharp.Server;

public class Echo : WebSocketService
{
  protected override void OnMessage (MessageEventArgs e)
  {
    Send (e.Data);
  }
}

And if you would like to provide a chat service,

using System;
using WebSocketSharp;
using WebSocketSharp.Server;

public class Chat : WebSocketService
{
  private string _suffix;

  public Chat ()
    : this (null)
  {
  }

  public Chat (string suffix)
  {
    _suffix = suffix ?? String.Empty;
  }

  protected override void OnMessage (MessageEventArgs e)
  {
    Sessions.Broadcast (e.Data + _suffix);
  }
}

If you override the WebSocketService.OnMessage (MessageEventArgs) method, that overridden method is called when the OnMessage event of the current session's WebSocket occurs.

And if you override the WebSocketService.OnOpen (), WebSocketService.OnError (ErrorEventArgs), and WebSocketService.OnClose (CloseEventArgs) methods, each of them is called when each event of the current session's WebSocket (the OnOpen, OnError, and OnClose events) occurs.

The WebSocketService.Send method sends a data to the client on the current session in the WebSocket service.

If you would like to access the sessions in the WebSocket service, you should use the WebSocketService.Sessions property (returns a WebSocketSharp.Server.WebSocketSessionManager).

The WebSocketService.Sessions.Broadcast method broadcasts a data to all clients of the WebSocket service.

Step 3

Creating an instance of the WebSocketServer class.

var wssv = new WebSocketServer (4649);
wssv.AddWebSocketService<Echo> ("/Echo");
wssv.AddWebSocketService<Chat> ("/Chat");
wssv.AddWebSocketService<Chat> ("/ChatWithNiceBoat", () => new Chat (" Nice boat."));

You can add any WebSocket service to your WebSocketServer with the specified path to the service, using the WebSocketServer.AddWebSocketService<TWithNew> (string) and WebSocketServer.AddWebSocketService<T> (string, Func<T>) methods.

The type of TWithNew must inherit the WebSocketService class and must have a public parameterless constructor.

The type of T must inherit the WebSocketService class.

So you can use the classes created in Step 2 to add the WebSocket service.

If you create an instance of the WebSocketServer class without a port number, the WebSocketServer set the port number to 80 automatically. So it's necessary to run with root permission.

$ sudo mono example2.exe

Step 4

Starting the WebSocket server.

wssv.Start ();

Step 5

Stopping the WebSocket server.

wssv.Stop (code, reason);

The WebSocketServer.Stop method is overloaded.

You can use the WebSocketServer.Stop (), WebSocketServer.Stop (ushort, string), or WebSocketServer.Stop (WebSocketSharp.CloseStatusCode, string) method to stop the server.

HTTP Server with the WebSocket

I modified the System.Net.HttpListener, System.Net.HttpListenerContext, and some other classes of Mono to create the HTTP server that allows to accept the WebSocket connection requests.

So websocket-sharp provides the WebSocketSharp.Server.HttpServer class.

You can add any WebSocket service to your HttpServer with the specified path to the service, using the HttpServer.AddWebSocketService<TWithNew> (string) and HttpServer.AddWebSocketService<T> (string, Func<T>) methods.

var httpsv = new HttpServer (4649);
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");
httpsv.AddWebSocketService<Chat> ("/ChatWithNiceBoat", () => new Chat (" Nice boat."));

For more information, could you see Example3?

WebSocket Extensions

Per-message Compression

websocket-sharp supports the Per-message Compression extension. (But it doesn't support with the extension parameters.)

If you would like to enable this extension as a WebSocket client, you should set like the following.

ws.Compression = CompressionMethod.DEFLATE;

And then your client sends the following header with the connection request to the server.

Sec-WebSocket-Extensions: permessage-deflate

If the server supports this extension, it returns the same header. And when your client receives that header, it enables this extension.

Secure Connection

websocket-sharp supports the Secure Connection (SSL).

As a WebSocket Client, you should create an instance of the WebSocket class with the wss scheme WebSocket URL to connect.

using (var ws = new WebSocket ("wss://example.com")) {
  ...
}

And if you would like to set the custom validation for the server certificate, you should set the WebSocket.ServerCertificateValidationCallback property.

ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
  // Do something to validate the server certificate.
  return true; // If the server certificate is valid.
};

If you set this property to nothing, the validation does nothing with the server certificate and returns valid.

As a WebSocket Server, you should create an instance of the WebSocketServer or HttpServer class with some settings for the secure connection. It's like the following.

var wssv = new WebSocketServer (4649, true);
wssv.Certificate = new X509Certificate2 ("/path/to/cert.pfx", "password for cert.pfx");

HTTP Authentication

websocket-sharp supports the HTTP Authentication (Basic/Digest).

As a WebSocket Client, you should set a pair of user name and password for the HTTP authentication, using the WebSocket.SetCredentials (string, string, bool) method before connecting.

ws.SetCredentials (username, password, preAuth);

If preAuth is true, the WebSocket sends the Basic authentication credentials with the first connection request to the server.

Or if preAuth is false, the WebSocket sends either the Basic or Digest authentication (determined by the unauthorized response to the first connection request) credentials with the second connection request to the server.

As a WebSocket Server, you should set an HTTP authentication scheme, a realm, and any function to find the user credentials before starting. It's like the following.

wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
wssv.Realm = "WebSocket Test";
wssv.UserCredentialsFinder = identity => {
  var expected = "nobita";
  return identity.Name == expected
         ? new NetworkCredential (expected, "password", "gunfighter") // User name, password, and roles
         : null; // If the user credentials not found.
};

If you would like to provide the Digest authentication, you should set like the following.

wssv.AuthenticationSchemes = AuthenticationSchemes.Digest;

Logging

The WebSocket class includes the own logging function.

You can access it with the WebSocket.Log property (returns a WebSocketSharp.Logger).

So if you would like to change the current logging level (WebSocketSharp.LogLevel.ERROR as the default), you should set the WebSocket.Log.Level property to any of the LogLevel enum values.

ws.Log.Level = LogLevel.DEBUG;

This means a log with less than LogLevel.DEBUG isn't outputted.

And if you would like to output a log, you should use any of the output methods. The following outputs a log with LogLevel.DEBUG.

ws.Log.Debug ("This is a debug message.");

The WebSocketServer and HttpServer classes include the same logging function.

Examples

Examples using websocket-sharp.

Example

Example connects to the Echo server with the WebSocket.

Example1

Example1 connects to the Audio Data delivery server with the WebSocket. (But it's only implemented the chat feature, still unfinished.)

And Example1 uses Json.NET.

Example2

Example2 starts a WebSocket server.

Example3

Example3 starts an HTTP server that allows to accept the WebSocket connection requests.

Could you access to http://localhost:4649 to do WebSocket Echo Test with your web browser after Example3 running?

Supported WebSocket Specifications

websocket-sharp supports RFC 6455 and is based on the following WebSocket references.

Thanks for translating to japanese.

License

websocket-sharp is provided under The MIT License.

About

A C# implementation of the WebSocket protocol client and server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.8%
  • Shell 0.2%