-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cs
More file actions
133 lines (115 loc) · 4.62 KB
/
Server.cs
File metadata and controls
133 lines (115 loc) · 4.62 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
using Newtonsoft.Json;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperWebSocket;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WebSocketServer
{
public class Server
{
private SuperWebSocket.WebSocketServer _server = new SuperWebSocket.WebSocketServer();
// クライアントセッションを格納
private List<WebSocketSession> _clinets = new List<WebSocketSession>();
// ルームリスト
private List<string> _rooms = new List<string>();
public Server()
{
//コンフィグオブジェクト作成
RootConfig rootConfig = new RootConfig();
ServerConfig serverConfig = new ServerConfig()
{
Port = 2012,
Ip = "Any",
MaxConnectionNumber = 100,
Mode = SocketMode.Tcp,
Name = "Test WebSocket Server"
};
//サーバーオブジェクト作成&初期化
_server.Setup(rootConfig, serverConfig);
//イベントハンドラの設定
//接続
_server.NewSessionConnected += server_NewSessionConnected;
//メッセージ受信
_server.NewMessageReceived += server_NewMessageReceived;
//切断
_server.SessionClosed += server_SessionClosed;
}
public void Start()
{
//サーバー起動
Console.WriteLine("Start!");
_server.Start();
}
public void Stop()
{
//サーバー起動
Console.WriteLine("Stop!");
_server.Stop();
}
void server_NewSessionConnected(WebSocketSession session)
{
Console.WriteLine("Connected!!");
// 新クライアントとして格納
_clinets.Add(session);
}
void server_NewMessageReceived(WebSocketSession session, string value)
{
Console.WriteLine("Received [" + value + "]"); // {"eventName":"join_room","data":{"room":""}}
//// 全ユーザに送信
//Parallel.ForEach(_clinets, p => p.Send(value));
XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(value), XmlDictionaryReaderQuotas.Max);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JoinRoom));
JoinRoom aaa = (JoinRoom)serializer.ReadObject(xmlReader);
if (aaa.eventName == "join_room")
{
//rtc.on('join_room', function(data, socket) {
Console.WriteLine("join_room");
//var connectionsId = [];
//var roomList = rtc.rooms[data.room] || [];
//roomList.push(socket.id);
//rtc.rooms[data.room] = roomList;
Console.WriteLine("\tsession id [" + session.SessionID + "]");
_rooms.Add(session.SessionID);
List<string> ids = new List<string>();
foreach (string room in _rooms)
{// (var i = 0; i < roomList.length; i++) {
if (room == session.SessionID)
{
continue;
}
else
{
ids.Add(room);
NewPeerConnected msg = new NewPeerConnected();
Dictionary<string, List<string>> data = new Dictionary<string,List<string>>();
data["socketId"] = ids;
msg.data = data;
string json = msg.ToJson();
Console.WriteLine("\tnew_peer_connected [" + json + "]");
session.Send("new_peer_connected", json);
}
}
GetPeers msg2 = new GetPeers();
GetPeers.InnerData data2 = new GetPeers.InnerData();
data2.connections = ids;
data2.you = session.SessionID;
msg2.data = data2;
string json2 = msg2.ToJson();
Console.WriteLine("\tget_peers [" + json2 + "]");
session.Send("get_peers", json2);
}
}
void server_SessionClosed(WebSocketSession session, CloseReason reason)
{
Console.WriteLine("Closed...");
// 該当クライアントの除外
_clinets.Remove(session);
}
}
}