首页 \ 问答 \ Netty代理示例中ctx.write(Unpooled.EMPTY_BUFFER)的用途是什么(What's the purpose of ctx.write(Unpooled.EMPTY_BUFFER) in Netty proxy example)

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 method channelActive 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
更新时间:2023-04-18 10:04

最满意答案

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. ...
  • 通常的做法是使用异步模块。 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对象并将其导出。 在控制 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)