using (var ws = new WebSocket("ws://dragonsnest.far/Laputa"))
using (var ws = new WebSocket("ws://dragonsnest.far/Laputa"))
{
{
ws.OnMessage += (sender, e) =>
ws.OnMessage += (sender, e) =>
{
{
Console.WriteLine("Laputa says: {0}", e.Data);
Console.WriteLine("Laputa says: {0}", e.Data);
};
};
ws.Connect();
ws.Connect();
ws.Send("BALUS");
ws.Send("BALUS");
Console.ReadKey(true);
Console.ReadKey(true);
}
}
}
}
}
}
@ -48,7 +48,7 @@ The `WebSocket` class exists in the `WebSocketSharp` namespace.
Creating a instance of the `WebSocket` class with the specified WebSocket URL to connect.
Creating a instance of the `WebSocket` class with the specified WebSocket URL to connect.
```cs
```cs
using (var ws = new WebSocket("ws://example.com"))
using (var ws = new WebSocket("ws://example.com"))
{
{
...
...
}
}
@ -84,11 +84,11 @@ ws.OnMessage += (sender, e) =>
};
};
```
```
`e.Type` (`WebSocketSharp.MessageEventArgs.Type`, the type of this property is `WebSocketSharp.Opcode`) indicates the **Frame Type** of a WebSocket frame, so by checking this property, you determine which item you should use.
`e.Type` (`WebSocketSharp.MessageEventArgs.Type`, its type is `WebSocketSharp.Opcode`) indicates the **Frame Type** of a received WebSocket frame. So by checking it, you determine which item you should use.
If `e.Type` equals `Opcode.TEXT`, you use `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, the type of this property is `string`) that contains the received **Text** data.
If `e.Type` equals `Opcode.TEXT`, you use `e.Data` (`WebSocketSharp.MessageEventArgs.Data`, its type is `string`) that contains a received **Text** data.
If `e.Type` equals `Opcode.BINARY`, you use `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, the type of this property is `byte[]`) that contains the received **Binary** data.
If `e.Type` equals `Opcode.BINARY`, you use `e.RawData` (`WebSocketSharp.MessageEventArgs.RawData`, its type is `byte []`) that contains a received **Binary** data.
```cs
```cs
if (e.Type == Opcode.TEXT)
if (e.Type == Opcode.TEXT)
@ -114,7 +114,7 @@ ws.OnError += (sender, e) =>
...
...
};
};
```
```
`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, the type of this property is `string`) contains an error message, so you use this.
`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`) contains an error message, so you use it.
##### WebSocket.OnClose Event #####
##### WebSocket.OnClose Event #####
@ -127,14 +127,14 @@ ws.OnClose += (sender, e) =>
};
};
```
```
`e.Code` (`WebSocketSharp.CloseEventArgs.Code`, the type of this property is `ushort`) contains a status code indicating the reason for closure and `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, the type of this property is `string`) contains the reason for closure, so you use these.
`e.Code` (`WebSocketSharp.CloseEventArgs.Code`, its type is `ushort`) contains a status code indicating the reason for closure and `e.Reason` (`WebSocketSharp.CloseEventArgs.Reason`, its type is `string`) contains the reason for closure, so you use them.
#### Step 4 ####
#### Step 4 ####
Connecting to the WebSocket server.
Connecting to the WebSocket server.
```cs
```cs
ws.Connect();
ws.Connect();
```
```
#### Step 5 ####
#### Step 5 ####
@ -142,26 +142,26 @@ ws.Connect();
Sending a data.
Sending a data.
```cs
```cs
ws.Send(data);
ws.Send(data);
```
```
The `Send` method is overloaded.
The `Send` method is overloaded.
The types of `data` are `string`, `byte[]` and `FileInfo` class.
The types of `data` are `string`, `byte[]` and `FileInfo` class.
#### Step 6 ####
#### Step 6 ####
Closing the WebSocket connection.
Closing the WebSocket connection.
```cs
```cs
ws.Close(code, reason);
ws.Close(code, reason);
```
```
If you want to close the WebSocket connection explicitly, you can use the `Close` method.
If you want to close the WebSocket connection explicitly, you use the `Close` method.
And the `Close` method is overloaded. The types of `code` are `WebSocketSharp.CloseStatusCode` and `ushort`, the type of `reason` is `string`.
The `Close` method is overloaded. The types of `code` are `WebSocketSharp.CloseStatusCode` and `ushort`, the type of `reason` is `string`.
In addition, the `Close()` and `Close(code)` methods exist.
In addition, the `Close()` and `Close(code)` methods exist.
@ -251,15 +251,15 @@ In addition, if you override the `OnOpen`, `OnError` and `OnClose` methods, each
Creating a instance of the `WebSocketServiceHost<T>` class if you want the single WebSocket service server.
Creating a instance of the `WebSocketServiceHost<T>` class if you want the single WebSocket service server.
```cs
```cs
var wssv = new WebSocketServiceHost<Echo>("ws://example.com:4649");
var wssv = new WebSocketServiceHost<Echo>("ws://example.com:4649");
```
```
Or creating a instance of the `WebSocketServer` class if you want the multi WebSocket service server.
Or creating a instance of the `WebSocketServer` class if you want the multi WebSocket service server.
```cs
```cs
var wssv = new WebSocketServer(4649);
var wssv = new WebSocketServer(4649);
wssv.AddWebSocketService<Echo>("/Echo");
wssv.AddWebSocketService<Echo>("/Echo");
wssv.AddWebSocketService<Chat>("/Chat");
wssv.AddWebSocketService<Chat>("/Chat");
```
```
You can add any WebSocket service with a specified path to the service to your `WebSocketServer` by using the `WebSocketServer.AddWebSocketService<T>` method.
You can add any WebSocket service with a specified path to the service to your `WebSocketServer` by using the `WebSocketServer.AddWebSocketService<T>` method.
@ -285,7 +285,7 @@ wssv.OnError += (sender, e) =>
};
};
```
```
`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, the type of this property is `string`) contains an error message, so you use this.
`e.Message` (`WebSocketSharp.ErrorEventArgs.Message`, its type is `string`) contains an error message, so you use it.
##### WebSocketServer.OnError Event #####
##### WebSocketServer.OnError Event #####
@ -296,7 +296,7 @@ Same as the `WebSocketServiceHost<T>.OnError` event.
Starting the server.
Starting the server.
```cs
```cs
wssv.Start();
wssv.Start();
```
```
#### Step 6 ####
#### Step 6 ####
@ -304,7 +304,7 @@ wssv.Start();
Stopping the server.
Stopping the server.
```cs
```cs
wssv.Stop();
wssv.Stop();
```
```
### HTTP Server with the WebSocket ###
### HTTP Server with the WebSocket ###
@ -314,8 +314,8 @@ I modified the `System.Net.HttpListener`, `System.Net.HttpListenerContext` and s
You can add any WebSocket service with a specified path to the service to your `HttpServer` by using the `HttpServer.AddWebSocketService<T>` method.
You can add any WebSocket service with a specified path to the service to your `HttpServer` by using the `HttpServer.AddWebSocketService<T>` method.
```cs
```cs
var httpsv = new HttpServer(4649);
var httpsv = new HttpServer(4649);
httpsv.AddWebSocketService<Echo>("/");
httpsv.AddWebSocketService<Echo>("/");
```
```
For more information, could you see **[Example3]**?
For more information, could you see **[Example3]**?
@ -325,13 +325,13 @@ For more information, could you see **[Example3]**?
As a **WebSocket Client**, creating a instance of the `WebSocket` class with the WebSocket URL with **wss** scheme.
As a **WebSocket Client**, creating a instance of the `WebSocket` class with the WebSocket URL with **wss** scheme.
```cs
```cs
using (var ws = new WebSocket("wss://example.com"))
using (var ws = new WebSocket("wss://example.com"))
{
{
...
...
}
}
```
```
If you want to set the custom validation for the server certificate, you can use the `WebSocket.ServerCertificateValidationCallback` property.
If you want to set the custom validation for the server certificate, you use the `WebSocket.ServerCertificateValidationCallback` property.