Ejemplo n.º 1
0
        public async Task Send(Message message)
        {
            IServiceCallback callback =
                OperationContext.Current.GetCallbackChannel <IServiceCallback>();
            IChannel channel = (IChannel)callback;

            WcfClientContext context;

            if (!callbacks.TryGetValue(channel, out context))
            {
                context            = new WcfClientContext(callback);
                callbacks[channel] = context;

                context.Closed += delegate()
                {
                    callbacks.TryRemove(channel, out context);
                };

                ClientForm clientForm = new ClientForm(context);
                clientForm.Show();
            }

            byte[] body = new byte[] { };
            if (!message.IsEmpty)
            {
                body = message.GetBody <byte[]>();
            }

            WebSocketMessageProperty property =
                (WebSocketMessageProperty)message.Properties[webSocketMessageProperty];

            await context.Receive(body, body.Length, property == null?
                                  WebSocketMessageType.Binary : property.MessageType, true);
        }
Ejemplo n.º 2
0
        public void Receive(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            WebSocketMessageProperty property =
                (WebSocketMessageProperty)message.Properties["WebSocketMessageProperty"];
            WebSocketContext context = property.WebSocketContext;

            string content = string.Empty;

            if (!message.IsEmpty)
            {
                byte[] body = message.GetBody <byte[]>();
                content = Encoding.UTF8.GetString(body);
            }


            string str;

            if (string.IsNullOrEmpty(content))
            {
                str = "连接成功";
            }
            else
            {
                str = "发送消息: " + content;
            }
            callback.Send(CreateMessage(str));
        }
Ejemplo n.º 3
0
        void IWebSocket.OnOpen()
        {
            WebSocketMessageProperty webSocketMessageProperty = (WebSocketMessageProperty)OperationContext.Current.IncomingMessageProperties["WebSocketMessageProperty"];

            this.WebSocketContext = webSocketMessageProperty.WebSocketContext;
            this.QueryParameters  = ParseQueryString(this.WebSocketContext.RequestUri.Query);
            this.OnOpen();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 消息事件处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public Task OnMessage(Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            WebSocketMessageProperty webSocketMessageProperty = (WebSocketMessageProperty)message.Properties["WebSocketMessageProperty"];

            byte[] body = message.GetBody <byte[]>();
            if (webSocketMessageProperty.MessageType == WebSocketMessageType.Binary)
            {
                this.OnMessage(body);
            }
            else
            {
                if (webSocketMessageProperty.MessageType != WebSocketMessageType.Text)
                {
                    throw new InvalidOperationException("Unknown message type");
                }
                string @string = Encoding.UTF8.GetString(body);
                this.OnMessage(@string);
            }
            return(Task.FromResult <int>(0));
        }