Netty代理示例中ctx.write(Unpooled.EMPTY_BUFFER)的用途是什么(What's the purpose of ctx.write(Unpooled.EMPTY_BUFFER) in Netty proxy example)
在Netty 代理示例的
HexDumpProxyBackendHandler
中,channelActive
方法的代码如下:public void channelActive(ChannelHandlerContext ctx) { ctx.read(); ctx.write(Unpooled.EMPTY_BUFFER); }
我的问题是
ctx.write(Unpooled.EMPTY_BUFFER)
的目的是什么? 在通道激活后是否需要发送空的TCP消息帧?In
HexDumpProxyBackendHandler
of Netty proxy example , the code of the methodchannelActive
likes following:public void channelActive(ChannelHandlerContext ctx) { ctx.read(); ctx.write(Unpooled.EMPTY_BUFFER); }
My question is what's the purpose of
ctx.write(Unpooled.EMPTY_BUFFER)
? Is it necessary to send an empty TCP message frame after the channel active?
原文:https://stackoverflow.com/questions/33030331
最满意答案
Node.js是制作实时框架以将用户链接到RESTful后端的不错选择。 但是,您也可以考虑使用托管实时消息服务(如PubNub)实时在用户和PHP后端之间传递数据。
使用PubNub的PHP Api ,您可以设置服务器以侦听事件:
$pubnub = new Pubnub( "demo", ## PUBLISH_KEY "demo", ## SUBSCRIBE_KEY "", ## SECRET_KEY false ## SSL_ON? ); $pubnub->subscribe(array( 'channel' => 'hello_world', ## REQUIRED Channel to Listen 'callback' => function($message) { ## REQUIRED Callback With Response ## Do all the awesome stuff your server does return true; ## Keep listening (return false to stop) } ));
现在您的服务器已订阅您的频道,您也可以让您的客户订阅,以收听全球事件。 我将从JavaScript SDK中给出一个示例,但是每个大型移动平台都有一个SDK:
var pubnub = PUBNUB.init({ publish_key : 'demo', subscribe_key : 'demo' }) function publish() { pubnub.publish({ channel : "hello_world", message : "Bob added Stan as a friend" }) } })();
您也可以反向执行此操作,以将消息从服务器广播到客户端。 巴姆!
最终,您可能希望为每个用户扩展您的应用程序,以便能够与服务器进行私密通信,以及身份验证; 我们称之为PubNub Access Manager,它受到了很大的支持。
祝你好运!
Node.js is a good choice to make a realtime framework to link your users to your RESTful backend. However, you may also consider using a hosted realtime messaging service such as PubNub to pass data between your users and your PHP backend in real time.
Using PubNub’s PHP Api, you can set up your server to listen for events:
$pubnub = new Pubnub( "demo", ## PUBLISH_KEY "demo", ## SUBSCRIBE_KEY "", ## SECRET_KEY false ## SSL_ON? ); $pubnub->subscribe(array( 'channel' => 'hello_world', ## REQUIRED Channel to Listen 'callback' => function($message) { ## REQUIRED Callback With Response ## Do all the awesome stuff your server does return true; ## Keep listening (return false to stop) } ));
Now that your server is subscribed to your channel, you can have your clients subscribe as well, to listen for global events. I’m going to give an example from the JavaScript SDK, but there’s an SDK for every sizable mobile platform as well:
var pubnub = PUBNUB.init({ publish_key : 'demo', subscribe_key : 'demo' }) function publish() { pubnub.publish({ channel : "hello_world", message : "Bob added Stan as a friend" }) } })();
You can also do this in reverse, to broadcast messages from the server to the clients. Bam!
Eventually you may want to extend your app with a unique channel for each user to be able to communicate privately with the server, and also authentication; we call this PubNub Access Manager and it is heavily supported.
Good luck!
相关问答
更多-
您无法使用Express获取内容,您应该使用Mikeal的请求库来实现该特定目的。 该库的API非常简单: var request = require('request'); request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Print the google web page. ...
-
Node.JS等待发出HTTP请求的REST Service的回调(Node.JS Wait for callback of REST Service that makes HTTP request)[2022-09-28]
通常的做法是使用异步模块。 npm install async async模块具有原语来处理各种形式的异步事件。 在你的情况下, async#parallel调用将允许你同时向所有外部API发出请求,然后将结果合并返回给你的请求者。 由于您正在提出外部http请求,因此您可能会发现请求模块也很有用。 npm install request 使用request和async#parallel你的路由处理程序看起来像这样... var request = require('request'); var asy ... -
Node本身没有施加TCP连接限制。 (总而言之,它是高度并发的,可以处理数千个并发连接。)您的操作系统可能会限制TCP连接。 您可能会遇到某种限制您的后台服务器,或者您正在打入内置HTTP库的连接限制,但很难说没有关于该服务器或Node实现的更多细节。 节点的内置HTTP库 (显然,任何建立在它上面的库,大多数)是通过Agent类维护一个连接池,以便它可以利用HTTP保持活动。 当您向同一台服务器运行许多请求时,这有助于提高性能,而不是打开TCP连接,进行HTTP请求,获取响应,关闭TCP连接以及重复; ...
-
对于nodejs中的流控制,我建议你使用async 如果你想并行执行而没有执行顺序: async.parallel([ function(callback) { request(apiCall1Options, callback); }, function(callback) { request(apiCall2Options, callback); } ], function(err, apiCallResults) { console.log(apiCallResults ...
-
像这样 app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,c ...
-
你可能不只是从正在进行的HTTP GET调用中返回一些东西。 这就是它超时的原因。 如果错误未eval为true,则需要发回HTTP 200,例如: res.send(); You're probably not just returning something from the HTTP GET call you're doing. That's why it times out. You need to send back a HTTP 200 if error does not eval to tr ...
-
给出的所有答案对于解决请求方面的缓存问题都是合理的。 但是,由于您指定了API.AI和Actions,因此您可能还需要在对话进行时存储信息。 您可以使用API.AI上下文执行此操作。 甚至可能是如果您将其限制为仅针对来自用户的每个响应的一个远程调用,您可能能够在时间范围内适应它。 例如,如果您正在进行有关电影时间和故障单排序的对话,则对话可能会像: 用户:“我想看电影。” [您使用API查找最近的剧院,将剧院的位置存储在上下文中并回复]“您最近的剧院是Mall Megaplex。您对那里有兴趣吗?” 用户: ...
-
由于您使用NodeJS作为Web服务器,因此请使用IP地址API密钥类型而不是“无”。 Since you are using NodeJS as web server, use the IP Addresses API key type instead of None.
-
Node.js是制作实时框架以将用户链接到RESTful后端的不错选择。 但是,您也可以考虑使用托管实时消息服务(如PubNub)实时在用户和PHP后端之间传递数据。 使用PubNub的PHP Api ,您可以设置服务器以侦听事件: $pubnub = new Pubnub( "demo", ## PUBLISH_KEY "demo", ## SUBSCRIBE_KEY "", ## SECRET_KEY false ## SSL_ON? ); $pubn ...
-
在我看来,如果你想构建复杂的或大的API,Express是一个很好的方法。 它可以轻松测试(例如使用Mocha或Jasmine)并可自定义,特别是由于它的中间件 。 对于目录结构,我通常使用的是(至少)以下内容: app.js :主要入口点。 将创建快速应用程序,指示每个路由前缀使用哪个控制器,并分配中间件。 上一个项目的示例 控制器 :将包含控制器,将处理请求的函数,与标准MVC框架(例如UserController,...)中的样式相同。 每个控制器都会创建一个快速的Router对象并将其导出。 在控制 ...