Пример #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            CseConfig             = new OM2MCseConfig();
            CseConfig.CseBaseId   = "in-cse";
            CseConfig.CseBaseName = "in-cse";
            CseConfig.CseType     = OM2MCseTypeID.InCSE;

            CseService = new OM2MCseServiceImpl(CseConfig);
            CseService.Initialize();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseWebSockets();

            app.Use(async(http, next) =>
            {
                string org = "guest:guest";
                Microsoft.Extensions.Primitives.StringValues vals;
                if (http.Request.Query.TryGetValue("org", out vals))
                {
                    org = vals[0];
                }

                if (http.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await http.WebSockets.AcceptWebSocketAsync();

                    var originator = new OM2MOriginatorWebSocket(CseService, org, webSocket);
                    while (webSocket.State == WebSocketState.Open)
                    {
                        var token    = CancellationToken.None;
                        var buffer   = new ArraySegment <Byte>(new byte[64 * 1024]);
                        var received = await webSocket.ReceiveAsync(buffer, token);

                        try
                        {
                            switch (received.MessageType)
                            {
                            case WebSocketMessageType.Text:
                                var message = Encoding.UTF8.GetString(buffer.Array,
                                                                      buffer.Offset,
                                                                      buffer.Count);
                                await originator.ProcessMessageAsync(message);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.Error.WriteLine(ex);
                        }
                    }
                }
                else
                {
                    await next();
                }
            });

            app.Run(async(context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
Пример #2
0
 public OM2MOriginatorWebSocket(OM2MCseServiceImpl cseService, string originatorId, WebSocket webSocket)
 {
     CseService   = cseService;
     OriginatorId = originatorId;
     WebSocket    = webSocket;
 }