Exemplo n.º 1
0
        /// <summary>
        /// 处理消息数据
        /// </summary>
        /// <param name="data">消息数据</param>
        /// <returns></returns>
        public async Task Excute(string data)
        {
            GlobalData.Logger.Log($"接收的数据{data}");

            var jobj    = new JsonObject(data);
            var toUser  = jobj["toUser"].Value;
            var toParty = jobj["toParty"].Value;
            var toTag   = jobj["toTag"].Value;
            var msgType = jobj["msgType"].Value;

            var result = new object();

            //解析文本
            if (msgType == "text")
            {
                var content = jobj["text"]["content"].Value;
                result = new { FromUser = UserName, MsgType = "text", Text = new { Content = content } };
            }
            //解析图片
            else if (msgType == "image")
            {
                var url = jobj["image"]["url"].Value;
                result = new { FromUser = UserName, MsgType = "image", Image = new { Url = url } };
            }
            //解析文件
            else if (msgType == "file")
            {
                var url = jobj["file"]["url"].Value;
                result = new { FromUser = UserName, MsgType = "file", File = new { Url = url, Type = "rar" } };
            }
            //解析语音
            else if (msgType == "voice")
            {
                var url = jobj["voice"]["url"].Value;
                result = new { FromUser = UserName, MsgType = "voice", File = new { Url = url } };
            }
            //解析视频
            else if (msgType == "video")
            {
                var url = jobj["video"]["url"].Value;
                result = new { FromUser = UserName, MsgType = "video", File = new { Url = url } };
            }
            await WsServer.Send(result, toUser, toParty, toTag);
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            //使用页面压缩
            app.UseResponseCompression();

            //使用页面缓存
            app.UseResponseCaching();

            //使用 Cookie 授权
            app.UseAuthentication();

            //使用 Session
            app.UseSession();

            app.UseStaticFiles();

            //设置跨域
            app.UseCors(builder =>
                        builder.WithOrigins("*").
                        WithMethods("*").
                        WithHeaders("*").
                        WithExposedHeaders("*"));

            //设置静态目录为本项目文件夹
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(GlobalData.EnvConfig.Path)
            });

            //配置 WebSocket 服务
            app.UseWebSockets(new WebSocketOptions()
            {
                KeepAliveInterval = TimeSpan.FromSeconds(120),
                ReceiveBufferSize = 4 * 1024
            });

            //启用 WebSocket 服务
            app.Use(async(context, next) =>
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var user = await WsServer.CreateAsync(context);
                    await user.Echo();
                }
                else
                {
                    await next();
                }
            });

            //启用 MVC
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            //启用 SignalR 服务
            app.UseSignalR(route =>
            {
                route.MapHub <ChatHub>("/chat");
            });
        }