+

Welcome to websocket-sharp!

websocket-sharp supports:

@@ -101,9 +82,11 @@ @@ -122,16 +105,16 @@ namespace Example { - public class Program + public class Program { - public static void Main (string[] args) + public static void Main (string[] args) { - using (var ws = new WebSocket ("ws://dragonsnest.far/Laputa")) { + using (var ws = new WebSocket ("ws://dragonsnest.far/Laputa")) { ws.OnMessage += (sender, e) => - Console.WriteLine ("Laputa says: " + e.Data); + Console.WriteLine ("Laputa says: " + e.Data); ws.Connect (); - ws.Send ("BALUS"); + ws.Send ("BALUS"); Console.ReadKey (true); } } @@ -152,7 +135,7 @@

Creating a new instance of the WebSocket class with the WebSocket URL to connect.

-
using (var ws = new WebSocket ("ws://example.com")) {
+
using (var ws = new WebSocket ("ws://example.com")) {
   ...
 }
@@ -245,15 +228,15 @@

Step 5

-

Sending a data to the WebSocket server.

+

Sending data to the WebSocket server.

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 a data.

+

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 a data asynchronously, you should use the WebSocket.SendAsync method.

+

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

ws.SendAsync (data, completed);
@@ -283,24 +266,24 @@ namespace Example { - public class Laputa : WebSocketBehavior + public class Laputa : WebSocketBehavior { - protected override void OnMessage (MessageEventArgs e) + protected override void OnMessage (MessageEventArgs e) { - var msg = e.Data == "BALUS" - ? "I've been balused already..." - : "I'm not available now."; + var msg = e.Data == "BALUS" + ? "I've been balused already..." + : "I'm not available now."; Send (msg); } } - public class Program + public class Program { - public static void Main (string[] args) + public static void Main (string[] args) { - var wssv = new WebSocketServer ("ws://dragonsnest.far"); - wssv.AddWebSocketService<Laputa> ("/Laputa"); + var wssv = new WebSocketServer ("ws://dragonsnest.far"); + wssv.AddWebSocketService<Laputa> ("/Laputa"); wssv.Start (); Console.ReadKey (true); wssv.Stop (); @@ -328,9 +311,9 @@ using WebSocketSharp; using WebSocketSharp.Server; -public class Echo : WebSocketBehavior +public class Echo : WebSocketBehavior { - protected override void OnMessage (MessageEventArgs e) + protected override void OnMessage (MessageEventArgs e) { Send (e.Data); } @@ -342,21 +325,21 @@ using WebSocketSharp; using WebSocketSharp.Server; -public class Chat : WebSocketBehavior +public class Chat : WebSocketBehavior { - private string _suffix; + private string _suffix; - public Chat () + public Chat () : this (null) { } - public Chat (string suffix) + public Chat (string suffix) { _suffix = suffix ?? String.Empty; } - protected override void OnMessage (MessageEventArgs e) + protected override void OnMessage (MessageEventArgs e) { Sessions.Broadcast (e.Data + _suffix); } @@ -364,25 +347,25 @@

You can define the behavior of any WebSocket service by creating the class that inherits the WebSocketBehavior class.

-

If you override the WebSocketBehavior.OnMessage (MessageEventArgs) method, it's called when the WebSocket used in the current session in the service receives a message.

+

If you override the WebSocketBehavior.OnMessage (MessageEventArgs) method, it's called when the WebSocket used in a session in the service receives a message.

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

-

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

+

The WebSocketBehavior.Send method sends data to the client on a session in the service.

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

-

The WebSocketBehavior.Sessions.Broadcast method broadcasts a data to every client in the service.

+

The WebSocketBehavior.Sessions.Broadcast method sends data to every client in the service.

Step 3

Creating a new instance of the WebSocketServer class.

-
var wssv = new WebSocketServer (4649);
-wssv.AddWebSocketService<Echo> ("/Echo");
-wssv.AddWebSocketService<Chat> ("/Chat");
-wssv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));
+
var wssv = new WebSocketServer (4649);
+wssv.AddWebSocketService<Echo> ("/Echo");
+wssv.AddWebSocketService<Chat> ("/Chat");
+wssv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));

You can add any WebSocket service to your WebSocketServer with the specified behavior and path to the service, by using the WebSocketServer.AddWebSocketService<TBehaviorWithNew> (string) or WebSocketServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>) method.

@@ -424,10 +407,10 @@ wssv.AddWebSocketService<Chat> ("

You can add any WebSocket service to your HttpServer with the specified behavior and path to the service, by using the HttpServer.AddWebSocketService<TBehaviorWithNew> (string) or HttpServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>) method.

-
var httpsv = new HttpServer (4649);
-httpsv.AddWebSocketService<Echo> ("/Echo");
-httpsv.AddWebSocketService<Chat> ("/Chat");
-httpsv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));
+
var httpsv = new HttpServer (4649);
+httpsv.AddWebSocketService<Echo> ("/Echo");
+httpsv.AddWebSocketService<Chat> ("/Chat");
+httpsv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));

For more information, would you see Example3?

@@ -437,7 +420,7 @@ httpsv.AddWebSocketService<Chat> ( Per-message Compression