60 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
using Aspose.Words.Lists;
 | 
						|
using Fleck;
 | 
						|
using Newtonsoft.Json;
 | 
						|
using StackExchange.Redis;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Linq;
 | 
						|
using System.Security.Cryptography.X509Certificates;
 | 
						|
using System.Web;
 | 
						|
 | 
						|
namespace WebAPI.Common
 | 
						|
{
 | 
						|
    public class PersonKqSocketSersvices
 | 
						|
    {
 | 
						|
        public static WebSocketServer webSocketServer;
 | 
						|
        public static Dictionary<String, IWebSocketConnection> allSockets;
 | 
						|
        public static void  init(int port)
 | 
						|
        {
 | 
						|
            allSockets = new Dictionary<string, IWebSocketConnection>();
 | 
						|
            webSocketServer = new WebSocketServer("wss://127.0.0.1:" + port);
 | 
						|
            webSocketServer.Certificate = new X509Certificate2("D:\\SGGLAPI\\bin\\wzgl.hfnbd.com.pfx", "1y1fib61");
 | 
						|
 | 
						|
            webSocketServer.Start(socket =>
 | 
						|
            {
 | 
						|
                socket.OnOpen = () =>
 | 
						|
                { 
 | 
						|
                };
 | 
						|
                socket.OnClose = () =>
 | 
						|
                {
 | 
						|
                    string socketKey = null;
 | 
						|
                    foreach(var key in allSockets.Keys)
 | 
						|
                    {
 | 
						|
                       if(allSockets[key] == socket)
 | 
						|
                        {
 | 
						|
                            socketKey = key;
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                    if (socketKey != null)
 | 
						|
                    {
 | 
						|
                        allSockets.Remove(socketKey);
 | 
						|
                    }
 | 
						|
                };
 | 
						|
                socket.OnMessage = message =>
 | 
						|
                {
 | 
						|
                    Dictionary<string, object> msg = JsonConvert.DeserializeObject<Dictionary<string,object>>(message);
 | 
						|
                    string projectid = msg["projectid"].ToString();
 | 
						|
                    if (allSockets.ContainsKey(projectid))
 | 
						|
                    {
 | 
						|
                        allSockets[projectid] = socket;
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        allSockets.Add(projectid , socket);
 | 
						|
                    }
 | 
						|
                    socket.Send("newinout");
 | 
						|
                };
 | 
						|
            });
 | 
						|
        }
 | 
						|
    }
 | 
						|
} |