Created
February 2, 2020 10:27
-
-
Save ksasao/73f7db78fad80f70b6b2fb807ebf5f44 to your computer and use it in GitHub Desktop.
C# WebSocket サーバ/ クライアントのサンプル。サーバ、クライアントそれぞれで、標準入力を受け付けてメッセージを送信します。.NET Core 3 / .NET Framework 向けのコンソールアプリです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WebSocket Client | |
using System; | |
using WebSocket4Net; | |
// NuGet で WebSocket4Net を追加してください | |
namespace WebSocketClientTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var ws = new WebSocket("ws://localhost:8000"); | |
ws.MessageReceived += Ws_MessageReceived; | |
ws.DataReceived += Ws_DataReceived; | |
ws.Opened += Ws_Opened; | |
ws.Closed += Ws_Closed; | |
ws.Open(); | |
while (true) | |
{ | |
var str = Console.ReadLine(); | |
if (str == "exit") | |
{ | |
break; | |
} | |
if (ws.State == WebSocketState.Open) | |
{ | |
ws.Send(str); | |
} | |
else | |
{ | |
Console.WriteLine("{0}:wait...", DateTime.Now.ToString()); | |
} | |
} | |
ws.Close(); | |
} | |
private static void Ws_Closed(object sender, EventArgs e) | |
{ | |
Environment.Exit(0); | |
} | |
private static void Ws_Opened(object sender, EventArgs e) | |
{ | |
Console.WriteLine("Opened"); | |
} | |
private static void Ws_DataReceived(object sender, DataReceivedEventArgs e) | |
{ | |
Console.WriteLine($"{e.Data.Length} bytes received."); | |
} | |
private static void Ws_MessageReceived(object sender, MessageReceivedEventArgs e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WebSocket Server | |
using Fleck; | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
// NuGet で Fleck を追加してください | |
namespace WebSocketServer | |
{ | |
class Program | |
{ | |
static object lockObj = new object(); | |
static List<IWebSocketConnection> sockets = new List<IWebSocketConnection>(); | |
static void Main(string[] args) | |
{ | |
var server = new Fleck.WebSocketServer("ws://0.0.0.0:8000"); | |
server.Start(socket => | |
{ | |
socket.OnOpen = () => OnOpen(socket); | |
socket.OnClose = () => OnClose(socket); | |
socket.OnMessage = message => OnMessage(socket, message); | |
}); | |
string data = ""; | |
while(!data.StartsWith("exit")) | |
{ | |
data = Console.ReadLine(); | |
SendMessageToAll(data); | |
} | |
foreach (var s in sockets) | |
{ | |
s.Close(); | |
} | |
server.Dispose(); | |
} | |
static async void OnOpen(IWebSocketConnection socket) | |
{ | |
lock (lockObj) | |
{ | |
sockets.Add(socket); | |
} | |
var info = socket.ConnectionInfo; | |
Console.WriteLine($"Open: {info.ClientIpAddress} {info.ClientPort}"); | |
for (int i = 0; i != 10; ++i) | |
{ | |
await Task.Delay(2000); | |
var time = DateTime.Now.ToLongTimeString(); | |
if (socket.IsAvailable) | |
{ | |
await socket.Send(time); | |
} | |
} | |
} | |
static void OnClose(IWebSocketConnection socket) | |
{ | |
var info = socket.ConnectionInfo; | |
Console.WriteLine($"Closed {info.ClientIpAddress} {info.ClientPort}"); | |
} | |
static void OnMessage(IWebSocketConnection socket, string message) | |
{ | |
var info = socket.ConnectionInfo; | |
Console.WriteLine($"Received from {info.ClientIpAddress} {info.ClientPort}: {message}"); | |
SendMessageToAll(message); | |
} | |
static void SendMessageToAll(string message) | |
{ | |
lock (lockObj) | |
{ | |
// remove unused sockets | |
List<IWebSocketConnection> soc = new List<IWebSocketConnection>(); | |
foreach (var s in sockets) | |
{ | |
if (s.IsAvailable) | |
{ | |
soc.Add(s); | |
} | |
sockets = soc; | |
} | |
// send message | |
foreach (var s in sockets) | |
{ | |
s.Send(message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment