Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ConsoleApp2 {
- class ServerV200 {
- List<IWebSocketConnection> allSockets;
- Guid id;
- Thread thread;
- static void Main(string[] args) {
- new ServerV200().socket();
- }
- public void socket() {
- allSockets = new List<IWebSocketConnection>();
- var server = new WebSocketServer("ws://0.0.0.0:2208");
- server.Start(socket => {
- socket.OnOpen = () => {
- Console.WriteLine("OnOpen()");
- allSockets.Add(socket);
- id = socket.ConnectionInfo.Id;
- thread = new Thread(delegate () { WriteY(true, id); });
- thread.Start();
- };
- socket.OnClose = () => {
- Console.WriteLine("OnClose()");
- //thread.Abort(true);
- //thread.Join();
- WriteY(false, id);
- //Console.WriteLine("OnClose() thread: " + thread);
- allSockets.Remove(socket);
- };
- socket.OnMessage = message => {
- Console.WriteLine(message);
- allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
- };
- });
- var input = Console.ReadLine();
- while (input != "exit") {
- foreach (var socket in allSockets.ToList()) {
- socket.Send(input);
- }
- input = Console.ReadLine();
- }
- }
- void SendToSocketById(String input, Guid id) {
- Console.WriteLine("input "+input+" "+id);
- var socket = allSockets.Find(client => client.ConnectionInfo.Id == id);
- socket.Send(input);
- }
- public void WriteY(bool action, Guid id) {
- int i = 0;
- while (action) {
- SendToSocketById(""+i, id);
- i++;
- Thread.Sleep(2000);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement