Ejemplo n.º 1
0
        /// <summary>
        /// 启动客户端并自动连接服务器
        /// </summary>
        public async ValueTask StartClient()
        {
            if (!clientReady)
            {
                return;
            }
            //检查是否已有服务器被启动
            if (NetUtils.serviceExitis)
            {
                throw new SoraClientIsRuningException();
            }
            //消息接收订阅
            Client.MessageReceived.Subscribe(msg => Task.Run(() =>
            {
                this.Event
                .Adapter(JObject.Parse(msg.Text), clientId);
            }));
            Client.DisconnectionHappened.Subscribe(info => Task.Run(() =>
            {
                ConnectionManager.GetLoginUid(clientId,
                                              out var uid);
                //移除原连接信息
                if (ConnectionManager.ConnectionExitis(clientId)
                    )
                {
                    ConnManager.CloseConnection("Universal",
                                                uid, clientId);
                }

                if (info.Exception != null)
                {
                    Log.Error("Sora",
                              $"监听服务器时发生错误{Log.ErrorLogBuilder(info.Exception)}");
                }
                else
                {
                    Log.Info("Sora", "服务器连接被关闭");
                }
            }));
            Client.ReconnectionHappened.Subscribe(info => Task.Run(() =>
            {
                if (info.Type == ReconnectionType.Initial)
                {
                    return;
                }
                Log.Info("Sora", "服务器已自动重连");
                ConnManager.OpenConnection("Universal", "0",
                                           Client, clientId);
            }));
            await Client.Start();

            if (!Client.IsRunning || !Client.IsStarted)
            {
                throw new WebSocketClientException("WebSocket client is not running");
            }

            ConnManager.OpenConnection("Universal", "0", Client, clientId);
            Log.Info("Sora", "Sora WebSocket客户端正在运行并已连接至onebot服务器");
            //启动心跳包超时检查计时器
            this.HeartBeatTimer = new Timer(ConnManager.HeartBeatCheck, null,
                                            Config.HeartBeatTimeOut, Config.HeartBeatTimeOut);
            NetUtils.serviceExitis = true;
            await Task.Delay(-1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 启动WS服务端
        /// </summary>
        /// <exception cref="SoraServerIsRuningException">已有服务器在运行</exception>
        public async ValueTask StartServer()
        {
            if (!serverReady)
            {
                return;
            }
            //检查是否已有服务器被启动
            if (NetUtils.serviceExitis)
            {
                throw new SoraServerIsRuningException();
            }
            //启动服务器
            Server.Start(socket =>
            {
                //接收事件处理
                //获取请求头数据
                if (!socket.ConnectionInfo.Headers.TryGetValue("X-Self-ID",
                                                               out var selfId) ||          //bot UID
                    !socket.ConnectionInfo.Headers.TryGetValue("X-Client-Role",
                                                               out var role))              //Client Type
                {
                    return;
                }

                //请求路径检查
                var isLost = role switch
                {
                    "Universal" => !socket.ConnectionInfo.Path.Trim('/')
                    .Equals(Config.UniversalPath.Trim('/')),
                    _ => true
                };
                if (isLost)
                {
                    socket.Close();
                    Log.Warning("Sora",
                                $"关闭与未知客户端的连接[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}],请检查是否设置正确的监听地址");
                    return;
                }

                //打开连接
                socket.OnOpen = () =>
                {
                    //获取Token
                    if (socket.ConnectionInfo.Headers.TryGetValue("Authorization",
                                                                  out var headerValue))
                    {
                        var token = headerValue.Split(' ')[1];
                        Log.Debug("Server", $"get token = {token}");
                        //验证Token
                        if (!token.Equals(this.Config.AccessToken))
                        {
                            return;
                        }
                    }

                    //向客户端发送Ping
                    socket.SendPing(new byte[] { 1, 2, 5 });
                    //事件回调
                    ConnManager.OpenConnection(role, selfId, socket,
                                               socket.ConnectionInfo.Id);
                    Log.Info("Sora",
                             $"已连接客户端[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}]");
                };
                //关闭连接
                socket.OnClose = () =>
                {
                    //移除原连接信息
                    if (ConnectionManager.ConnectionExitis(socket.ConnectionInfo.Id))
                    {
                        ConnManager.CloseConnection(role, Convert.ToInt64(selfId),
                                                    socket.ConnectionInfo.Id);
                    }

                    Log.Info("Sora",
                             $"客户端连接被关闭[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}]");
                };
                //上报接收
                socket.OnMessage = (message) =>
                {
                    //进入事件处理和分发
                    Task.Run(() =>
                    {
                        this.Event
                        .Adapter(JObject.Parse(message),
                                 socket.ConnectionInfo.Id);
                    });
                };
            });
            Log.Info("Sora", $"Sora WebSocket服务器正在运行[{Config.Host}:{Config.Port}]");
            //启动心跳包超时检查计时器
            this.HeartBeatTimer = new Timer(ConnManager.HeartBeatCheck, null,
                                            Config.HeartBeatTimeOut, Config.HeartBeatTimeOut);
            NetUtils.serviceExitis = true;

            await Task.Delay(-1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 启动WS服务端
        /// </summary>
        /// <exception cref="SoraServerIsRuningException">已有服务器在运行</exception>
        public async ValueTask StartServer()
        {
            if (!serverReady)
            {
                return;
            }
            //检查是否已有服务器被启动
            if (serverExitis)
            {
                throw new SoraServerIsRuningException();
            }
            //启动服务器
            Server.Start(socket =>
            {
                //接收事件处理
                //获取请求头数据
                if (!socket.ConnectionInfo.Headers.TryGetValue("X-Self-ID",
                                                               out var selfId) ||          //bot UID
                    !socket.ConnectionInfo.Headers.TryGetValue("X-Client-Role",
                                                               out var role))              //Client Type
                {
                    return;
                }

                //请求路径检查
                var isLost = role switch
                {
                    "Universal" => !socket.ConnectionInfo.Path.Trim('/').Equals(Config.UniversalPath),
                    _ => true
                };
                if (isLost)
                {
                    socket.Close();
                    Log.Warning("Sora",
                                $"关闭与未知客户端的连接[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}],请检查是否设置正确的监听地址");
                    return;
                }

                //打开连接
                socket.OnOpen = () =>
                {
                    //获取Token
                    if (socket.ConnectionInfo.Headers.TryGetValue("Authorization",
                                                                  out string token))
                    {
                        //验证Token
                        if (!token.Equals(this.Config.AccessToken))
                        {
                            return;
                        }
                    }

                    //向客户端发送Ping
                    socket.SendPing(new byte[] { 1, 2, 5 });
                    //事件回调
                    ConnManager.OpenConnection(role, selfId, socket);
                    Log.Info("Sora",
                             $"已连接客户端[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}]");
                };
                //关闭连接
                socket.OnClose = () =>
                {
                    //移除原连接信息
                    if (ConnectionManager.ConnectionExitis(socket.ConnectionInfo.Id))
                    {
                        ConnManager.CloseConnection(role, selfId, socket);
                    }

                    Log.Info("Sora",
                             $"客户端连接被关闭[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}]");
                };
                //上报接收
                socket.OnMessage = (message) =>
                {
                    //处理接收的数据
                    if (!ConnectionManager.ConnectionExitis(socket.ConnectionInfo.Id))
                    {
                        return;
                    }
                    //进入事件处理和分发
                    Task.Run(() =>
                    {
                        this.Event
                        .Adapter(JObject.Parse(message),
                                 socket.ConnectionInfo.Id);
                    });
                };
            });
            Log.Info("Sora", $"Sora WebSocket服务器正在运行[{Config.Location}:{Config.Port}]");
            Log.Info("Sora", $"Sora 服务端框架版本:{Assembly.GetExecutingAssembly().GetName().Version}");
            //启动心跳包超时检查计时器
            this.HeartBeatTimer = new Timer(ConnManager.HeartBeatCheck, null,
                                            new TimeSpan(0, 0, 0, (int)Config.HeartBeatTimeOut, 0),
                                            new TimeSpan(0, 0, 0, (int)Config.HeartBeatTimeOut, 0));
            serverExitis = true;

            Log.Debug("Sora", "开发交流群:1081190562");

            await Task.Delay(-1);
        }