xiazhengtongxue
8 小时以前 af4404160a9e8d14c09f1e6acab2ba00cb7fc91b
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
using HslCommunication.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Helper;
 
namespace WIDESEA_Core.Extensions
{
    public static class WebSocketSetup
    {
        //public static void AddWebSocketSetup(this IServiceCollection services)
        //{
        //    if (services == null) throw new ArgumentNullException(nameof(services));
 
        //    int port = AppSettings.Get("WebSocketPort").ObjToInt();
        //    if (port == 0)
        //    {
        //        port = 9296;
        //    }
        //    services.AddSingleton(x =>
        //    {
        //        WebSocketServer socketServer = new WebSocketServer();
        //        socketServer.ServerStart(port);
        //        return socketServer;
        //    });
        //}
        public static void AddWebSocketSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
 
            int port = AppSettings.Get("WebSocketPort").ObjToInt();
            if (port == 0)
            {
                port = 9296;
            }
 
            // 直接创建并启动 WebSocket 服务器
            WebSocketServer socketServer = new WebSocketServer();
            socketServer.ServerStart(port);
            services.AddSingleton(socketServer);
 
        }
    }
}