static void StartWebSocketServer(int _websocketPort) { string url = "ws://localhost:" + _websocketPort.ToString(); WebSocketServer server = new WebSocketServer(url); server.Start(socket => { string originurl = socket.ConnectionInfo.Host + socket.ConnectionInfo.Path; socket.OnOpen = () => { Console.WriteLine(originurl + " connected"); if (socket.ConnectionInfo.Path == "/Client") { addClient(socket, ClientList); Debug.WriteLine("Client ++ => " + ClientList.Count.ToString()); } command c = new command("nodeDes", strNodesDes); socket.Send(JsonConvert.SerializeObject(c) as string); }; socket.OnClose = () => { Console.WriteLine(originurl + " closed"); removeClient(socket, ClientList); }; socket.OnMessage = message => { Debug.WriteLine("OnMessage => " + message); //List<TagInfo> tags = TagPool.GetAllExistsTags(); //string json = JsonConvert.SerializeObject(tags); //socket.Send(json); if (message == "nodeDes") { command c = new command("nodeDes", strNodesDes); socket.Send(JsonConvert.SerializeObject(c) as string); } if (message == "nodes") { command c = new command("nodes", NodeInfoParser.GetNodesJson()); socket.Send(JsonConvert.SerializeObject(c) as string); //socket.Send(NodeInfoParser.GetNodesJson()); } }; socket.OnError = (error) => { Debug.WriteLine("OnError => " + error.Data); removeClient(socket, ClientList); }; }); }
static void ReportChangedNode(Node _node) { List <Node> list = new List <Node> { _node }; string json = JsonConvert.SerializeObject(list); Debug.WriteLine("Node {0} Changed!", _node.nodeId); command c = new command("nodes", json); string sToSend = (JsonConvert.SerializeObject(c) as string); List <IWebSocketConnection> clientList = new List <IWebSocketConnection>(ClientList); foreach (IWebSocketConnection socket in clientList) { socket.Send(sToSend); } }