Servicestack 服务器发送事件 [英] Servicestack server sent events

查看:46
本文介绍了Servicestack 服务器发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始搞乱我自己的 ServiceStack 服务器事件实现.

I just started messing with my own implementation of ServiceStack server events.

在阅读 wiki 部分并阅读聊天应用程序的代码后,我开始创建自己的新 mvc4 项目并通过 nuGet 安装所有 ServiceStack 库.

After reading the wiki section and reading the code of the chat application, I started creating my own new mvc4 project and installing all the ServiceStack libraries via nuGet.

在配置和创建 AppHost 之后,我创建了一个新的helloworld"服务并启动了站点,只是为了确保它有效(确实有效).

After configuring and creating the AppHost, I created a new "helloworld" service and started the site, just to make sure it worked (it worked indeed).

现在是主要问题.基于现有的示例代码(聊天应用程序),我创建了新的 HTML 页面和调用 ServerEvents.NotifyChannel 的服务,然后我启动了新站点.

Now the main issues. Based on the existing example code (the chat application), I created the new HTML page and the service which calls ServerEvents.NotifyChannel and then I started the new site.

当通过 ajax (post) 调用服务时,调试器会遇到断点,但在服务返回时不会触发客户端事件.更重要的是,通知似乎有点缓存/延迟",当第二次调用服务时,客户端从第一次调用接收数据.

When calling the service via ajax (post) the debugger hits the break point, but the client events aren't fired when the service returns. Even more, it seems that the notify is sort of "cached/delayed" and when calling the service the second time, the client receives the data from the first call.

我应该使用 servicestack.razor 视图吗?我是否应该创建另一个 MVC 应用程序,然后从头开始构建所有内容以对其进行测试?

Should I use servicestack.razor views? Should I create another MVC application and again, build everything from scratch to test it?

编辑这篇文章以添加代码(如所指出的):https://github.com/xspadax/SS_SSE_Test

editing this post to add code (as pointed out): https://github.com/xspadax/SS_SSE_Test

推荐答案

这个问题是由于 Flush()ASP.NET MVC 项目中不能正常工作,看起来启用压缩时自动缓冲,可以通过以下方式禁用动态页面:

This issue is due to Flush() not working properly in ASP.NET MVC projects which seems to automatically buffer when compression is enabled, it can be disabled for dynamic pages with:

<system.webServer>
  <urlCompression doStaticCompression="true" doDynamicCompression="false" />
</system.webServer>

顺便说一句,当启用压缩时,即使是用于缓冲输出的显式 API 似乎也没有任何影响:

Incidentally not even the explicit API for Buffering output seems to have any effect when compression is enabled:

Response.BufferOutput = false;

不使用 ASP.NET MVC 的普通 ASP.NET 项目,如 ServiceStack + Razor 不使用有这个问题.

Normal ASP.NET projects that don't use ASP.NET MVC like ServiceStack + Razor doesn't have this issue.

v4.0.32+ 中添加了新 API这是 现在可在 MyGet 上使用,为 ServiceStack 的服务器事件功能提供更多自定义挂钩,OnInit 可用于修改服务器事件流的 HTTP 标头,而 OnPublish() 回调在每条消息发布后触发:

New API's were added in v4.0.32+ that's now available on MyGet to provide more custom hooks into ServiceStack's Server Events Feature, OnInit can be used to modify the HTTP Headers of the Server Events Stream, whilst the OnPublish() callback is fired after every message is published:

Plugins.Add(new ServerEventsFeature { 
    /*
    OnInit = req => { //fired when sending headers in server event stream
        var res = (HttpResponseBase)req.Response.OriginalResponse; //Get MVC Response
        res.BufferOutput = false; // Should make Flush() work, but doesn't
    },
    */
    OnPublish = (res, msg) => { //fired after ever message is published
        res.Write("\n\n\n\n\n\n\n\n\n\n");
        res.Flush();    
    }
});

上述解决方案涉及在 MVC 中编写许多尾随 \n 行以强制刷新.这是一个有效的替代解决方案,因为在 Server Sent Events 中会忽略尾随的新行.

The solution above involves writing a number of trailing \n lines to force a flush in MVC. It's a valid alternative solution as trailing new lines are ignored in Server Sent Events.

这篇关于Servicestack 服务器发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆