Beispiel #1
0
        private void _server_OnReceive(object currentObj, byte[] data)
        {
            var ut = (WSUserToken)(currentObj);

            try
            {
                if (!ut.IsHandSharked)
                {
                    byte[] resData = null;

                    var result = ut.GetReplayHandShake(data, out resData);

                    if (result)
                    {
                        _server.SendAsync(ut.ID, resData);
                        ut.IsHandSharked = true;
                        lock (_locker)
                            Clients.Add(ut.ID);
                        OnConnected?.Invoke(ut.ID);
                    }
                }
                else
                {
                    var coder = (WSCoder)ut.Unpacker;
                    coder.Unpack(data, (d) =>
                    {
                        var wsProtocal = (WSProtocal)d;
                        switch (wsProtocal.Type)
                        {
                        case (byte)WSProtocalType.Close:
                            ReplyClose(ut.ID, wsProtocal);
                            break;

                        case (byte)WSProtocalType.Ping:
                            ReplyPong(ut.ID, wsProtocal);
                            break;

                        case (byte)WSProtocalType.Binary:
                        case (byte)WSProtocalType.Text:
                        case (byte)WSProtocalType.Cont:
                            OnMessage?.Invoke(ut.ID, (WSProtocal)d);
                            break;

                        case (byte)WSProtocalType.Pong:
                            break;

                        default:
                            var error = string.Format("收到未定义的Opcode={0}", d.Type);
                            break;
                        }
                    }, (h) => { }, null);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("WServer.OnReceive.Error", ex);
                _server.Disconnect(ut.ID);
            }
        }
Beispiel #2
0
        private void ReplyClose(IUserToken ut, QueueResult data)
        {
            var qcoder = (QUnpacker)ut.Unpacker;

            _serverSokcet.Send(ut.ID, qcoder.QueueCoder.Close(data.Name));
            _exchange.Clear(ut.ID);
            _serverSokcet.Disconnect(ut.ID);
        }
Beispiel #3
0
 public void Disconnecte(IUserToken userToken)
 {
     _serverSokcet.Disconnect(userToken.ID);
 }
Beispiel #4
0
 internal void Disconnect(IUserToken userToken)
 {
     _server.Disconnect(userToken.ID);
 }
Beispiel #5
0
        private void _server_OnReceive(object currentObj, byte[] data)
        {
            var mUserToken = (MessageUserToken)currentObj;

            mUserToken.Unpacker.Unpack(data, (s) =>
            {
                if (s.Content != null)
                {
                    try
                    {
                        var cm = SerializeHelper.PBDeserialize <ChatMessage>(s.Content);

                        switch (cm.Type)
                        {
                        case ChatMessageType.Login:
                            ReplyLogin(mUserToken, cm);
                            break;

                        case ChatMessageType.Subscribe:
                            ReplySubscribe(mUserToken, cm);
                            break;

                        case ChatMessageType.UnSubscribe:
                            ReplyUnsubscribe(mUserToken, cm);
                            break;

                        case ChatMessageType.ChannelMessage:
                            ReplyChannelMessage(mUserToken, cm);
                            break;

                        case ChatMessageType.PrivateMessage:
                            ReplyPrivateMessage(mUserToken, cm);
                            break;

                        case ChatMessageType.CreateGroup:
                            ReplyCreateGroup(mUserToken, cm);
                            break;

                        case ChatMessageType.AddMember:
                            ReplyAddMember(mUserToken, cm);
                            break;

                        case ChatMessageType.RemoveMember:
                            ReplyRemoveMember(mUserToken, cm);
                            break;

                        case ChatMessageType.RemoveGroup:
                            ReplyRemoveGroup(mUserToken, cm);
                            break;

                        case ChatMessageType.GroupMessage:
                            ReplyGroupMessage(mUserToken, cm);
                            break;

                        default:
                            throw new Exception("未知的协议");
                        }
                    }
                    catch (Exception ex)
                    {
                        _server.Disconnect(mUserToken.ID);
                        LogHelper.Error("MessageServer._server_OnReceive", ex);
                    }
                }
            }, null, null);
        }
Beispiel #6
0
 /// <summary>
 /// Disconnect
 /// </summary>
 /// <param name="id"></param>
 public void Disconnect(string id)
 {
     _serverSokcet.Disconnect(id);
 }