打扰一下,看了您在http://bbs.csdn.net/topics/390618779?page=1中的回复“等会Server发消息过来后会通知你消息到了,然后你再处理消息。” 这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?我想在客户端发送消息的代码之后,紧跟着写对应答消息的处理代码,例如: EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler(); b.group(group) .channel(NioSocketChannel.class) .handler(new UnionPayClientInitializer(handler)); ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
打扰一下,看了您在http://bbs.csdn.net/topics/390618779?page=1中的回复“等会Server发消息过来后会通知你消息到了,然后你再处理消息。” 这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?我想在客户端发送消息的代码之后,紧跟着写对应答消息的处理代码,例如: EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler(); b.group(group) .channel(NioSocketChannel.class) .handler(new UnionPayClientInitializer(handler)); ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
打扰一下,看了您在http://bbs.csdn.net/topics/390618779?page=1中的回复“等会Server发消息过来后会通知你消息到了,然后你再处理消息。” 这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?我想在客户端发送消息的代码之后,紧跟着写对应答消息的处理代码,例如: EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler(); b.group(group) .channel(NioSocketChannel.class) .handler(new UnionPayClientInitializer(handler)); ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
打扰一下,看了您在http://bbs.csdn.net/topics/390618779?page=1中的回复“等会Server发消息过来后会通知你消息到了,然后你再处理消息。” 这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?我想在客户端发送消息的代码之后,紧跟着写对应答消息的处理代码,例如: EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler(); b.group(group) .channel(NioSocketChannel.class) .handler(new UnionPayClientInitializer(handler)); ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
public void operationComplete(ChannelFuture future) {
}
这个地方是表示数据已经发送完毕。可以记录一些其他事情,比如日志。
发送完毕后,有新数据响应仍然走 Handler原来的流程。在这里面应该无法读取到响应数据。
(如果服务器同时向多个客户端,连续发送多条指令,每个不同的指令返回的数据不同,那么怎么确定该包为某一指令发出得到的数据)
这个地方 如果是多个客户端,那么每个客户端的ID是唯一的,netty里面有唯一ID号标示请求的唯一性。
还有一种方法是 发送请求时带一个唯一ID号,响应数据时也是带一个唯一ID号,这个唯一ID号就能确保客户端与服务器端的包是否一致性问题。
等会Server发消息过来后会通知你消息到了,然后你再处理消息。
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new UnionPayClientInitializer(handler));
ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
byte[] signIn = UnionPayBuildUtils.buildSignIn();
f.channel().writeAndFlush(Unpooled.copiedBuffer(signIn));
UnionPayParseUtils.parseAndKeepForSignIn(handler.queue().take()); // handler.queue().take()可以得到应答消息,handler.queue() 类型BlockingQueue<byte[]>
以上代码能保证应答消息是对应我本次的请求吗?如果不能,我应该加些什么代码?谢谢您的帮助。
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new UnionPayClientInitializer(handler));
ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
byte[] signIn = UnionPayBuildUtils.buildSignIn();
f.channel().writeAndFlush(Unpooled.copiedBuffer(signIn));
UnionPayParseUtils.parseAndKeepForSignIn(handler.queue().take()); // handler.queue().take()可以得到应答消息,handler.queue() 类型BlockingQueue<byte[]>
以上代码能保证应答消息是对应我本次的请求吗?如果不能,我应该加些什么代码?谢谢您的帮助。
不能,因为是异步的,可以给每个消息都加上个编号,回应的时候把消息的编号给带上。这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?
我记得好像是messageReceived方法会被调用,Netty只有几个方法需要看的,可以看看官方的例子,挺简单的。
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new UnionPayClientInitializer(handler));
ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
byte[] signIn = UnionPayBuildUtils.buildSignIn();
f.channel().writeAndFlush(Unpooled.copiedBuffer(signIn));
UnionPayParseUtils.parseAndKeepForSignIn(handler.queue().take()); // handler.queue().take()可以得到应答消息,handler.queue() 类型BlockingQueue<byte[]>
以上代码能保证应答消息是对应我本次的请求吗?如果不能,我应该加些什么代码?谢谢您的帮助。
不能,因为是异步的,可以给每个消息都加上个编号,回应的时候把消息的编号给带上。这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?
我记得好像是messageReceived方法会被调用,Netty只有几个方法需要看的,可以看看官方的例子,挺简单的。
假如我每次请求都是重建一次连接,也就是会new一次handler,那么应该返回的就是对应的应答吧?这样做合适吗?对内存占用大吗?
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
UnionPayClientSignInHandler handler = new UnionPayClientSignInHandler();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new UnionPayClientInitializer(handler));
ChannelFuture f = b.connect(INET_HOST, INET_PORT).sync();
byte[] signIn = UnionPayBuildUtils.buildSignIn();
f.channel().writeAndFlush(Unpooled.copiedBuffer(signIn));
UnionPayParseUtils.parseAndKeepForSignIn(handler.queue().take()); // handler.queue().take()可以得到应答消息,handler.queue() 类型BlockingQueue<byte[]>
以上代码能保证应答消息是对应我本次的请求吗?如果不能,我应该加些什么代码?谢谢您的帮助。
不能,因为是异步的,可以给每个消息都加上个编号,回应的时候把消息的编号给带上。这里Server发消息过来后会通知你消息到了是指handler中的channelRead0方法会被执行吗?
我记得好像是messageReceived方法会被调用,Netty只有几个方法需要看的,可以看看官方的例子,挺简单的。
假如我每次请求都是重建一次连接,也就是会new一次handler,那么应该返回的就是对应的应答吧?这样做合适吗?对内存占用大吗?
当然不合适,TCP连接建立是很消耗资源的。