Beispiel #1
0
        private async Task SubscriberLoop()
        {
            Int32 bufferSize = 1024 * 32;
            byte[] buffer = new byte[bufferSize];
            MemoryStream stream = new MemoryStream(bufferSize);
            Int32 start = 0;
            String msg = "";

            try
            {
                while (this._socket.State == WebSocketState.Open)
                {
                    ArraySegment<byte> segment = new ArraySegment<byte>(buffer);
                    WebSocketReceiveResult result = await this._socket.ReceiveAsync(segment, CancellationToken.None);
                    if (!result.EndOfMessage)
                    {
                        stream.Write(buffer, start, result.Count);
                        start += result.Count;
                    }
                    else
                    {
                        stream.Write(buffer, start, result.Count);
                        msg = Encoding.UTF8.GetString(stream.ToArray());
                        dynamic message = JsonConvert.DeserializeObject<dynamic>(msg);
                        
                        

                        if (this._sessionId == null)
                            this._sessionId = message.session;
                        else if(message == null || message.msg == null)
                        {
                            if(message.server_id == null)
                                throw new DdpClientException(String.Format("Unknown message: {0}", message));
                        }
                        else
                        {
                            String msgType = message.msg.ToString();

                            switch (msgType)
                            {
                                case "ping":
                                    await this.Send(new { msg = "pong" });
                                    break;
                                case "error":
                                    if (this.MeteorError != null)
                                        this.MeteorError(this, new DdpMeteorErrorEventArgs(message.reason.ToString(), message.offendingMessage));
                                    break;
                                case "updated":
                                    if (this.MethodUpdated != null)
                                    {
                                        List<String> methods = new List<string>();
                                        foreach (var m in message.methods)
                                            methods.Add(m.ToString());
                                        this.MethodUpdated(this, new DdpMethodUpdatedEventArgs(methods.ToArray()));

                                    }
                                    break;
                                case "ready":
                                    break;
                                case "result":
                                    if (this.MethodResult != null)
                                    {
                                        Meteor.DDP.DdpMethodResultEventArgs.MethodError error = null;
                                        if (message.error != null)
                                        {
                                            error = new DdpMethodResultEventArgs.MethodError((Int32)message.error.error,
                                                message.error.reason.ToString(),
                                                message.error.message.ToString(),
                                                message.error.errorType.ToString());
                                        }
                                        this.MethodResult(this, new DdpMethodResultEventArgs(message.id.ToString(), error));
                                    }
                                    break;
                                default:
                                    if (message.id != null && message.msg != null && message.collection != null && message.fields != null)
                                    {
                                        if (this.Message != null)
                                            this.Message(this, new DdpMessageEventArgs(message.id.ToString(), message.msg.ToString(), message.collection.ToString(), message.fields));
                                    }
                                    else
                                        throw new DdpClientException("Unrecognized message: " + message.ToString());
                                    break;
                            }
                        }
                        stream.Dispose();
                        stream = new MemoryStream();

                    }

                }
            }
            catch (Exception x)
            {
                if (this.ClientError != null)
                    this.ClientError(this, new DdpClientErrorEventArgs(x));
            }
            finally
            {
                this._sessionId = null;
                stream.Dispose();
            }
        }
Beispiel #2
0
        private async Task SubscriberLoop()
        {
            Int32 bufferSize = 1024 * 32;

            byte[]       buffer = new byte[bufferSize];
            MemoryStream stream = new MemoryStream(bufferSize);
            Int32        start  = 0;
            String       msg    = "";

            try
            {
                while (this._socket.State == WebSocketState.Open)
                {
                    ArraySegment <byte>    segment = new ArraySegment <byte>(buffer);
                    WebSocketReceiveResult result  = await this._socket.ReceiveAsync(segment, CancellationToken.None);

                    if (!result.EndOfMessage)
                    {
                        stream.Write(buffer, start, result.Count);
                        start += result.Count;
                    }
                    else
                    {
                        stream.Write(buffer, start, result.Count);
                        msg = Encoding.UTF8.GetString(stream.ToArray());
                        dynamic message = JsonConvert.DeserializeObject <dynamic>(msg);



                        if (this._sessionId == null)
                        {
                            this._sessionId = message.session;
                        }
                        else if (message == null || message.msg == null)
                        {
                            if (message.server_id == null)
                            {
                                throw new DdpClientException(String.Format("Unknown message: {0}", message));
                            }
                        }
                        else
                        {
                            String msgType = message.msg.ToString();

                            switch (msgType)
                            {
                            case "ping":
                                await this.Send(new { msg = "pong" });

                                break;

                            case "error":
                                if (this.MeteorError != null)
                                {
                                    this.MeteorError(this, new DdpMeteorErrorEventArgs(message.reason.ToString(), message.offendingMessage));
                                }
                                break;

                            case "updated":
                                if (this.MethodUpdated != null)
                                {
                                    List <String> methods = new List <string>();
                                    foreach (var m in message.methods)
                                    {
                                        methods.Add(m.ToString());
                                    }
                                    this.MethodUpdated(this, new DdpMethodUpdatedEventArgs(methods.ToArray()));
                                }
                                break;

                            case "ready":
                                break;

                            case "result":
                                if (this.MethodResult != null)
                                {
                                    Meteor.DDP.DdpMethodResultEventArgs.MethodError error = null;
                                    if (message.error != null)
                                    {
                                        error = new DdpMethodResultEventArgs.MethodError((Int32)message.error.error,
                                                                                         message.error.reason.ToString(),
                                                                                         message.error.message.ToString(),
                                                                                         message.error.errorType.ToString());
                                    }
                                    this.MethodResult(this, new DdpMethodResultEventArgs(message.id.ToString(), error));
                                }
                                break;

                            default:
                                if (message.id != null && message.msg != null && message.collection != null && message.fields != null)
                                {
                                    if (this.Message != null)
                                    {
                                        this.Message(this, new DdpMessageEventArgs(message.id.ToString(), message.msg.ToString(), message.collection.ToString(), message.fields));
                                    }
                                }
                                else
                                {
                                    throw new DdpClientException("Unrecognized message: " + message.ToString());
                                }
                                break;
                            }
                        }
                        stream.Dispose();
                        stream = new MemoryStream();
                    }
                }
            }
            catch (Exception x)
            {
                if (this.ClientError != null)
                {
                    this.ClientError(this, new DdpClientErrorEventArgs(x));
                }
            }
            finally
            {
                this._sessionId = null;
                stream.Dispose();
            }
        }