diff --git a/index.html b/index.html index 46382646..34ced796 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@
using System;
-using WebSocketSharp;
+using System;
+using WebSocketSharp;
namespace Example
{
@@ -128,7 +128,7 @@
Required namespace.
-using WebSocketSharp;
+using WebSocketSharp;
The WebSocket
class exists in the WebSocketSharp
namespace.
@@ -137,7 +137,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")) {
...
}
@@ -153,7 +153,7 @@
A WebSocket.OnOpen
event occurs when the WebSocket connection has been established.
-ws.OnOpen += (sender, e) => {
+ws.OnOpen += (sender, e) => {
...
};
@@ -164,44 +164,44 @@
A WebSocket.OnMessage
event occurs when the WebSocket
receives a message.
-ws.OnMessage += (sender, e) => {
+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 message. So by checking it, you can determine which item you should use.
+If you would like to get the message data, you should access e.Data
or e.RawData
property.
-If it returns Opcode.Text
, you should use e.Data
property that returns a string
(represents the Text message).
+And you can determine which property you should access by checking e.IsText
or e.IsBinary
property.
-Or if it returns Opcode.Binary
, you should use e.RawData
property that returns a byte[]
(represents the Binary message).
+If e.IsText
is true
, you should access e.Data
that returns a string
(represents a text message).
-if (e.Type == Opcode.Text) {
+Or if e.IsBinary
is true
, you should access e.RawData
that returns a byte[]
(represents a binary message).
+
+if (e.IsText) {
// Do something with e.Data.
...
return;
}
-if (e.Type == Opcode.Binary) {
+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
, such as the following.
+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
, such as the following.
-ws.EmitOnPing = true;
+ws.EmitOnPing = true;
ws.OnMessage += (sender, e) => {
- if (e.Type == Opcode.Ping) {
- // Do something to notify that a Ping has been received.
+ if (e.IsPing) {
+ // Do something to notify that a ping has been received.
...
return;
}
-
- ...
};
@@ -209,7 +209,7 @@ ws.OnMessage += (sender, e) => {
A WebSocket.OnError
event occurs when the WebSocket
gets an error.
-ws.OnError += (sender, e) => {
+ws.OnError += (sender, e) => {
...
};
@@ -224,7 +224,7 @@ ws.OnMessage += (sender, e) => {
A WebSocket.OnClose
event occurs when the WebSocket connection has been closed.
-ws.OnClose += (sender, e) => {
+ws.OnClose += (sender, e) => {
...
};
@@ -237,7 +237,7 @@ ws.OnMessage += (sender, e) => {
Connecting to the WebSocket server.
-ws.Connect ();
+ws.Connect ();
If you would like to connect to the server asynchronously, you should use the WebSocket.ConnectAsync ()
method.
@@ -246,7 +246,7 @@ ws.OnMessage += (sender, e) => {
Sending data to the WebSocket server.
-ws.Send (data);
+ws.Send (data);
The WebSocket.Send
method is overloaded.
@@ -254,7 +254,7 @@ ws.OnMessage += (sender, e) => {
If you would like to send the data asynchronously, you should use the WebSocket.SendAsync
method.
-ws.SendAsync (data, completed);
+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.
@@ -263,7 +263,7 @@ ws.OnMessage += (sender, e) => {
Closing the WebSocket connection.
-ws.Close (code, reason);
+ws.Close (code, reason);
If you would like to close the connection explicitly, you should use the WebSocket.Close
method.
@@ -276,9 +276,9 @@ ws.OnMessage += (sender, e) => {
- WebSocket Serverusing System;
-using WebSocketSharp;
-using WebSocketSharp.Server;
+using System;
+using WebSocketSharp;
+using WebSocketSharp.Server;
namespace Example
{
@@ -312,7 +312,7 @@ ws.OnMessage += (sender, e) => {
Required namespace.
-using WebSocketSharp.Server;
+using WebSocketSharp.Server;
The WebSocketBehavior
and WebSocketServer
classes exist in the WebSocketSharp.Server
namespace.
@@ -323,9 +323,9 @@ ws.OnMessage += (sender, e) => {
For example, if you would like to provide an echo service,
-using System;
-using WebSocketSharp;
-using WebSocketSharp.Server;
+using System;
+using WebSocketSharp;
+using WebSocketSharp.Server;
public class Echo : WebSocketBehavior
{
@@ -337,9 +337,9 @@ ws.OnMessage += (sender, e) => {
And if you would like to provide a chat service,
-using System;
-using WebSocketSharp;
-using WebSocketSharp.Server;
+using System;
+using WebSocketSharp;
+using WebSocketSharp.Server;
public class Chat : WebSocketBehavior
{
@@ -369,7 +369,7 @@ ws.OnMessage += (sender, e) => {
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
).
+If you would like to get the sessions in the service, you should access the WebSocketBehavior.Sessions
property (returns a WebSocketSharp.Server.WebSocketSessionManager
).
The WebSocketBehavior.Sessions.Broadcast
method sends data to every client in the service.
@@ -378,7 +378,7 @@ ws.OnMessage += (sender, e) => {
Creating a new instance of the WebSocketServer
class.
-var wssv = new WebSocketServer (4649);
+var wssv = new WebSocketServer (4649);
wssv.AddWebSocketService<Echo> ("/Echo");
wssv.AddWebSocketService<Chat> ("/Chat");
wssv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));
@@ -401,14 +401,14 @@ wssv.AddWebSocketService<Chat> ("<
Starting the WebSocket server.
-wssv.Start ();
+wssv.Start ();
Step 5Stopping the WebSocket server.
-wssv.Stop (code, reason);
+wssv.Stop (code, reason);
The WebSocketServer.Stop
method is overloaded.
@@ -423,7 +423,7 @@ 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);
+var httpsv = new HttpServer (4649);
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");
httpsv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));
@@ -440,7 +440,7 @@ httpsv.AddWebSocketService<Chat> (
As a WebSocket client, if you would like to enable this extension, you should set such as the following.
-ws.Compression = CompressionMethod.Deflate;
+ws.Compression = CompressionMethod.Deflate;
And then your client will send the following header in the connection request to the server.
@@ -454,7 +454,7 @@ httpsv.AddWebSocketService<Chat> (
As a WebSocket server, if you would like to ignore the extensions requested from a client, you should set the WebSocketBehavior.IgnoreExtensions
property to true
in your WebSocketBehavior
constructor or initializing it, such as the following.
-wssv.AddWebSocketService<Chat> (
+wssv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat () {
// To ignore the extensions requested from a client.
@@ -472,13 +472,13 @@ httpsv.AddWebSocketService<Chat> (
As a WebSocket Client, you should create a new instance of the WebSocket
class with the wss scheme WebSocket URL.
-using (var ws = new WebSocket ("wss://example.com")) {
+using (var ws = new WebSocket ("wss://example.com")) {
...
}
And if you would like to use the custom validation for the server certificate, you should set the WebSocket.SslConfiguration.ServerCertificateValidationCallback
property.
-ws.SslConfiguration.ServerCertificateValidationCallback =
+ws.SslConfiguration.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => {
// Do something to validate the server certificate.
...
@@ -490,7 +490,7 @@ httpsv.AddWebSocketService<Chat> (
As a WebSocket Server, you should create a new instance of the WebSocketServer
or HttpServer
class with some settings for secure connection, such as the following.
-var wssv = new WebSocketServer (5963, true);
+var wssv = new WebSocketServer (5963, true);
wssv.SslConfiguration.ServerCertificate =
new X509Certificate2 ("/path/to/cert.pfx", "password for cert.pfx");
@@ -501,7 +501,7 @@ wssv.SslConfiguration.ServerCertificate =
As a WebSocket Client, you should set a pair of user name and password for the HTTP authentication, by using the WebSocket.SetCredentials (string, string, bool)
method before connecting.
-ws.SetCredentials ("nobita", "password", preAuth);
+ws.SetCredentials ("nobita", "password", preAuth);
If preAuth
is true
, the WebSocket
sends the Basic authentication credentials with the first connection request to the server.
@@ -509,7 +509,7 @@ wssv.SslConfiguration.ServerCertificate =
As a WebSocket Server, you should set an HTTP authentication scheme, a realm, and any function to find the user credentials before starting, such as the following.
-wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
+wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
wssv.Realm = "WebSocket Test";
wssv.UserCredentialsFinder = id => {
var name = id.Name;
@@ -522,28 +522,28 @@ wssv.UserCredentialsFinder = id => {
If you would like to provide the Digest authentication, you should set such as the following.
-wssv.AuthenticationSchemes = AuthenticationSchemes.Digest;
+wssv.AuthenticationSchemes = AuthenticationSchemes.Digest;
Query String, Origin header and CookiesAs a WebSocket Client, if you would like to send the Query String with the WebSocket connection request to the server, you should create a new instance of the WebSocket
class with the WebSocket URL that includes the Query string parameters.
-using (var ws = new WebSocket ("ws://example.com/?name=nobita")) {
+using (var ws = new WebSocket ("ws://example.com/?name=nobita")) {
...
}
And if you would like to send the Origin header with the WebSocket connection request to the server, you should set the WebSocket.Origin
property to an allowable value as the Origin header before connecting, such as the following.
-ws.Origin = "http://example.com";
+ws.Origin = "http://example.com";
And also if you would like to send the Cookies with the WebSocket connection request to the server, you should set any cookie by using the WebSocket.SetCookie (WebSocketSharp.Net.Cookie)
method before connecting, such as the following.
-ws.SetCookie (new Cookie ("name", "nobita"));
+ws.SetCookie (new Cookie ("name", "nobita"));
-As a WebSocket Server, if you would like to get the Query String included in each WebSocket connection request, you should access the WebSocketBehavior.Context.QueryString
property, such as the following.
+As a WebSocket Server, if you would like to get the Query String included in a WebSocket connection request, you should access the WebSocketBehavior.Context.QueryString
property, such as the following.
-public class Chat : WebSocketBehavior
+public class Chat : WebSocketBehavior
{
private string _name;
...
@@ -556,9 +556,9 @@ wssv.UserCredentialsFinder = id => {
...
}
-And if you would like to validate the Origin header, Cookies, or both included in each WebSocket connection request, you should set each validation with your WebSocketBehavior
, for example, by using the AddWebSocketService<TBehavior> (string, Func<TBehavior>)
method with initializing, such as the following.
+And if you would like to validate the Origin header, Cookies, or both included in a WebSocket connection request, you should set each validation with your WebSocketBehavior
, for example, by using the AddWebSocketService<TBehavior> (string, Func<TBehavior>)
method with initializing, such as the following.
-wssv.AddWebSocketService<Chat> (
+wssv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat () {
OriginValidator = val => {
@@ -589,7 +589,7 @@ wssv.UserCredentialsFinder = id => {
If you would like to connect to a WebSocket server through the HTTP Proxy server, you should set the proxy server URL, and if necessary, a pair of user name and password for the proxy server authentication (Basic/Digest), by using the WebSocket.SetProxy (string, string, string)
method before connecting.
-var ws = new WebSocket ("ws://example.com");
+var ws = new WebSocket ("ws://example.com");
ws.SetProxy ("http://localhost:3128", "nobita", "password");
I tested this with the Squid. And it's necessary to disable the following configuration option in squid.conf (e.g. /etc/squid/squid.conf
).
@@ -607,13 +607,13 @@ ws.SetProxy ("http://localhost:31
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;
+ws.Log.Level = LogLevel.Debug;
The above means a log with lower than LogLevel.Debug
cannot be 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.");
+ws.Log.Debug ("This is a debug message.");
The WebSocketServer
and HttpServer
classes include the same logging function.
@@ -644,7 +644,7 @@ ws.SetProxy ("http://localhost:31
Example3 starts an HTTP server that allows to accept the WebSocket connection requests.
-Would you access to http://localhost:4649 to do WebSocket Echo Test with your web browser after Example3 running?
+Would you access to http://localhost:4649 to do WebSocket Echo Test with your web browser while Example3 is running?
@@ -690,4 +690,3 @@ ws.SetProxy ( Supported WebSocket Specifications"http://localhost:31