websocket-sharp-master.zip

  • Valya
    了解作者
  • WINDOWS
    开发工具
  • 249KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-04-13 01:02
    上传日期
THis is unitypackage
websocket-sharp-master.zip
内容介绍
![Logo](websocket-sharp_logo.png) ## Welcome to websocket-sharp! ## websocket-sharp supports: - [RFC 6455](#supported-websocket-specifications) - [WebSocket Client](#websocket-client) and [Server](#websocket-server) - [Per-message Compression](#per-message-compression) extension - [Secure Connection](#secure-connection) - [HTTP Authentication](#http-authentication) - [Query string, Origin header, and Cookies](#query-string-origin-header-and-cookies) - [Connecting through the HTTP proxy server](#connecting-through-the-http-proxy-server) - .NET Framework **3.5** or later (includes compatible environment such as [Mono]) ## Branches ## - [master] for production releases. - [hybi-00] for older [draft-ietf-hybi-thewebsocketprotocol-00]. No longer maintained. - [draft75] for even more old [draft-hixie-thewebsocketprotocol-75]. No longer maintained. ## Build ## websocket-sharp is built as a single assembly, **websocket-sharp.dll**. websocket-sharp is developed with [MonoDevelop]. So a simple way to build is to open **websocket-sharp.sln** and run build for **websocket-sharp project** with any of the build configurations (e.g. `Debug`) in MonoDevelop. ## Install ## ### Self Build ### You should add your websocket-sharp.dll (e.g. `/path/to/websocket-sharp/bin/Debug/websocket-sharp.dll`) to the library references of your project. If you would like to use that dll in your [Unity] project, you should add it 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. - [NuGet Gallery: websocket-sharp] You can add websocket-sharp to your project with the NuGet Package Manager, by using 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 (Sorry, Not available now). - [WebSocket-Sharp for Unity] It works with **Unity Free**, but there are some limitations: - [Security Sandbox of the Webplayer] (The server is not available in Web Player) - [WebGL Networking] (Not available in WebGL) - Incompatible platform (Not available for such UWP) - Lack of dll for the System.IO.Compression (The compression extension is not available on Windows) - .NET Socket Support for iOS/Android (iOS/Android Pro is required if your Unity is earlier than Unity 5) - .NET API 2.0 compatibility level for iOS/Android .NET API 2.0 compatibility level for iOS/Android may require to fix lack of some features for later than .NET Framework 2.0, such as the `System.Func<...>` delegates (so i have added them in the asset package). And it is priced at **US$15**. I believe your $15 makes this project more better, **Thank you!** ## Usage ## ### WebSocket Client ### ```csharp 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. ```csharp using WebSocketSharp; ``` The `WebSocket` class exists in the `WebSocketSharp` namespace. #### Step 2 #### Creating a new instance of the `WebSocket` class with the WebSocket URL to connect. ```csharp var ws = new WebSocket ("ws://example.com"); ``` The `WebSocket` class inherits the `System.IDisposable` interface, so you can create it with the `using` statement. ```csharp using (var ws = new WebSocket ("ws://example.com")) { ... } ``` This will **close** the WebSocket connection with status code `1001` (going away) when the control leaves the `using` block. #### Step 3 #### Setting the `WebSocket` events. ##### WebSocket.OnOpen Event ##### This event occurs when the WebSocket connection has been established. ```csharp ws.OnOpen += (sender, e) => { ... }; ``` `System.EventArgs.Empty` is passed as `e`, so you do not need to use it. ##### WebSocket.OnMessage Event ##### This event occurs when the `WebSocket` receives a message. ```csharp ws.OnMessage += (sender, e) => { ... }; ``` A `WebSocketSharp.MessageEventArgs` instance is passed as `e`. If you would like to get the message data, you should access `e.Data` or `e.RawData` property. `e.Data` property returns a `string`, so it is mainly used to get the **text** message data. `e.RawData` property returns a `byte[]`, so it is mainly used to get the **binary** message data. ```csharp if (e.IsText) { // Do something with e.Data. ... return; } if (e.IsBinary) { // Do something with e.RawData. ... return; } ``` And if you would like to notify that a **ping** has been received, via this event, you should set the `WebSocket.EmitOnPing` property to `true`. ```csharp ws.EmitOnPing = true; ws.OnMessage += (sender, e) => { if (e.IsPing) { // Do something to notify that a ping has been received. ... return; } }; ``` ##### WebSocket.OnError Event ##### This event occurs when the `WebSocket` gets an error. ```csharp ws.OnError += (sender, e) => { ... }; ``` A `WebSocketSharp.ErrorEventArgs` instance is passed as `e`. If you would like to get the error message, you should access `e.Message` property. `e.Message` property returns a `string` that represents the error message. And `e.Exception` property returns a `System.Exception` instance that represents the cause of the error if it is due to an exception. ##### WebSocket.OnClose Event ##### This event occurs when the WebSocket connection has been closed. ```csharp ws.OnClose += (sender, e) => { ... }; ``` A `WebSocketSharp.CloseEventArgs` instance is passed as `e`. If you would like to get the reason for the close, you should access `e.Code` or `e.Reason` property. `e.Code` property returns a `ushort` that represents the status code for the close. `e.Reason` property returns a `string` that represents the reason for the close. #### Step 4 #### Connecting to the WebSocket server. ```csharp ws.Connect (); ``` If you would like to connect to the server asynchronously, you should use the `WebSocket.ConnectAsync ()` method. #### Step 5 #### Sending data to the WebSocket server. ```csharp ws.Send (data); ``` The `WebSocket.Send` method is overloaded. You can use the `WebSocket.Send (string)`, `WebSocket.Send (byte[])`, or `WebSocket.Send (System.IO.FileInfo)` method to send the data. If you would like to send the data asynchronously, you should use the `WebSocket.SendAsync` method. ```csharp ws.SendAsync (data, completed); ``` And also if you would like to do something when the send is complete, you should set `completed` to any `Action<bool>` delegate. #### Step 6 #### Closing the WebSocket connection. ```csharp 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 ### ```csharp using System; using WebSocketSharp; using WebSocketSharp.Server; namespace Example { public class Laputa : WebSocketBehavior { 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 = n
评论
    相关推荐
    • unity2020
      unity2020
    • Dlib unity
      基于dlib版unity的人脸识别,Dlib FaceLandmark Detector 1.2.0亲测可用
    • Unityfps
      Unityfps
    • Unity迷宫
      Unity迷宫
    • unitygame
      统一游戏
    • unity
      unity
    • Unity PerfAssist
      各种Unity profiler增强组件和代码,Various performance-related components for Unity development.
    • UNITY Shader
      拓展的unity shader 包,可以在sublime 进行基本的智能提示
    • unity itween
      unity itween插件,里面有itween.cs和itweenpath.unitypackage
    • unity二维码
      该项目使用unity实现二维码功能,生成二维码并支持扫描