-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cs
More file actions
139 lines (121 loc) · 4.82 KB
/
Server.cs
File metadata and controls
139 lines (121 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace WebSocketServer2
{
public class Server
{
HttpListener _listener = new HttpListener();
Thread _thread = null;
/// <summary>
/// クライアントのWebSocketインスタンスを格納
/// </summary>
HashSet<WebSocket> _clients = new HashSet<WebSocket>();
/// <summary>
/// 部屋を格納
/// </summary>
Dictionary<string, HashSet<WebSocket>> _rooms = new Dictionary<string, HashSet<WebSocket>>();
public Server()
{
_listener.Prefixes.Add("http://+:2013/");
}
public void Start()
{
ThreadStart action = async () =>
{
_listener.Start();
while (true)
{
var context = await _listener.GetContextAsync();
if (context.Request.IsWebSocketRequest)
{
ProcessRequest(context);
}
else
{
context.Response.StatusCode = 400;
context.Response.Close();
}
}
};
_thread = new Thread(action);
_thread.Start();
}
public void Stop()
{
_listener.Stop();
if (_thread != null)
{
_thread.Join();
_thread = null;
}
}
/// <summary>
/// WebSocket接続毎の処理
/// </summary>
/// <param name="context"></param>
async void ProcessRequest(HttpListenerContext context)
{
Console.WriteLine("{0}:New Session:{1}", DateTime.Now.ToString(), context.Request.RemoteEndPoint.Address.ToString());
/// WebSocketの接続完了を待機してWebSocketオブジェクトを取得する
var ws = (await (context.AcceptWebSocketAsync(null))).WebSocket;
/// 新規クライアントを追加
_clients.Add(ws);
/// WebSocketの送受信ループ
while (ws.State == WebSocketState.Open)
{
try
{
var buff = new ArraySegment<byte>(new byte[1024]);
/// 受信待機
var ret = await ws.ReceiveAsync(buff, CancellationToken.None);
/// テキスト
if (ret.MessageType == WebSocketMessageType.Text)
{
Console.WriteLine("{0}:String Received:{1}", DateTime.Now.ToString(), context.Request.RemoteEndPoint.Address.ToString());
string msgjson = Encoding.UTF8.GetString(buff.Take(ret.Count).ToArray());
Console.WriteLine("Message={0}", msgjson);
Message msg = MessageParser.Parse(msgjson);
if (msg.GetType() == typeof(JoinRoom))
{
HashSet<WebSocket> room = _rooms[ws.GetHashCode().ToString()];
room.Add(ws);
_rooms[ws.GetHashCode().ToString()] = room;
foreach (KeyValuePair<string, HashSet<WebSocket>> val in _rooms)
{
if (val.Key == ws.GetHashCode().ToString()) continue;
else
{
val.Value.Contains(ws);
}
}
}
/// 各クライアントへ配信
Parallel.ForEach(_clients, p => p.SendAsync(new ArraySegment<byte>(buff.Take(ret.Count).ToArray()), WebSocketMessageType.Text, true, CancellationToken.None));
}
else if (ret.MessageType == WebSocketMessageType.Close)
{
Console.WriteLine("{0}:Session Close:{1}", DateTime.Now.ToString(), context.Request.RemoteEndPoint.Address.ToString());
break;
}
}
catch
{
/// 例外 クライアントが異常終了しやがった
Console.WriteLine("{0}:Session Abort:{1}", DateTime.Now.ToString(), context.Request.RemoteEndPoint.Address.ToString());
break;
}
}
/// クライアントを除外する
_clients.Remove(ws);
ws.Dispose();
}
}
}