Beispiel #1
0
        /// <summary>
        /// 在长连接模式下,断开服务器的连接,并切换到短连接模式
        /// </summary>
        /// <returns>关闭连接,不需要查看IsSuccess属性查看</returns>
        public OperateResult ConnectClose( )
        {
            OperateResult result = new OperateResult( );

            IsPersistentConn = false;

            // 额外操作
            result = ExtraOnDisconnect(CoreSocket);
            // 关闭信息
            CoreSocket?.Close( );
            CoreSocket = null;

            LogNet?.WriteDebug(ToString( ), StringResources.NetEngineClose);
            return(result);
        }
        /// <summary>
        /// 在长连接模式下,断开服务器的连接,并切换到短连接模式
        /// </summary>
        /// <returns>关闭连接,不需要查看IsSuccess属性查看</returns>
        /// <example>
        /// 直接关闭连接即可,基本上是不需要进行成功的判定
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDoubleBase.cs" region="ConnectCloseExample" title="关闭连接结果" />
        /// </example>
        public OperateResult ConnectClose( )
        {
            OperateResult result = new OperateResult( );

            isPersistentConn = false;

            InteractiveLock.Enter( );
            // 额外操作
            result = ExtraOnDisconnect(CoreSocket);
            // 关闭信息
            CoreSocket?.Close( );
            CoreSocket = null;
            InteractiveLock.Leave( );

            LogNet?.WriteDebug(ToString( ), StringResources.Language.NetEngineClose);
            return(result);
        }
        /// <summary>
        /// 创建数据推送服务,使用事件绑定的机制实现
        /// </summary>
        /// <returns>是否创建成功</returns>
        public OperateResult CreatePush( )
        {
            CoreSocket?.Close( );

            // 连接服务器
            OperateResult <Socket> connect = CreateSocketAndConnect(endPoint, 5000);

            if (!connect.IsSuccess)
            {
                return(connect);
            }

            // 发送订阅的关键字
            OperateResult send = SendStringAndCheckReceive(connect.Content, 0, keyWord);

            if (!send.IsSuccess)
            {
                return(send);
            }

            // 确认服务器的反馈
            OperateResult <int, string> receive = ReceiveStringContentFromSocket(connect.Content);

            if (!receive.IsSuccess)
            {
                return(receive);
            }

            // 订阅不存在
            if (receive.Content1 != 0)
            {
                connect.Content?.Close( );
                return(new OperateResult(receive.Content2));
            }

            // 异步接收
            AppSession appSession = new AppSession( );

            CoreSocket            = connect.Content;
            appSession.WorkSocket = connect.Content;
            ReBeginReceiveHead(appSession, false);

            return(OperateResult.CreateSuccessResult( ));
        }
        private OperateResult CreatePush( )
        {
            CoreSocket?.Close( );

            OperateResult <Socket> connect = CreateSocketAndConnect(endPoint, 5000);

            if (!connect.IsSuccess)
            {
                return(connect);
            }

            OperateResult send = SendStringAndCheckReceive(connect.Content, 0, keyWord);

            if (!send.IsSuccess)
            {
                return(send);
            }

            OperateResult <int, string> receive = ReceiveStringContentFromSocket(connect.Content);

            if (!receive.IsSuccess)
            {
                return(receive);
            }

            if (receive.Content1 != 0)
            {
                return new OperateResult( )
                       {
                           Message = receive.Content2
                       }
            }
            ;

            AppSession appSession = new AppSession( );

            CoreSocket            = connect.Content;
            appSession.WorkSocket = connect.Content;
            ReBeginReceiveHead(appSession, false);

            return(OperateResult.CreateSuccessResult( ));
        }
Beispiel #5
0
 /// <summary>
 /// 发送字符串数据到服务器
 /// </summary>
 /// <param name="customer">用户自定义的标记数据</param>
 /// <param name="data">字符串数据</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="SocketException"></exception>
 /// <exception cref="ObjectDisposedException"></exception>
 public void SendMessage(NetHandle customer, string data)
 {
     CoreSocket.SendTo(HslProtocol.CommandBytes(customer, Token, data), ServerEndPoint);
 }
 /// <summary>
 /// 关闭消息推送的界面
 /// </summary>
 public void ClosePush( )
 {
     action = null;
     CoreSocket?.Close( );
 }