本文整理汇总了Java中io.netty.channel.ChannelHandlerContext.channel方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelHandlerContext.channel方法的具体用法?Java ChannelHandlerContext.channel怎么用?Java ChannelHandlerContext.channel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.ChannelHandlerContext
的用法示例。
在下文中一共展示了ChannelHandlerContext.channel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fixHandlerBeforeConnect
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
/**
* 适配
*/
@Override
protected ChannelHandler fixHandlerBeforeConnect(final ChannelHandler handler) {
ChannelHandler result=new ShareableChannelInboundHandler() {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
Channel ch=ctx.channel();
ch.pipeline().addLast(new HttpClientCodec());
ch.pipeline().addLast(new HttpObjectAggregator(64*1024));
ch.pipeline().addLast(new WebSocketClientProtocolHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())));
ch.pipeline().addLast(new WebSocketConnectedClientHandler(handler));
ctx.pipeline().remove(this);//移除当前handler
ctx.pipeline().fireChannelRegistered();//重新从第一个handler抛出事件
}
};
// ChannelInitializer<SocketChannel> result=new ChannelInitializer<SocketChannel>() {
// @Override
// protected void initChannel(SocketChannel ch) {
// ch.pipeline().addLast(new HttpClientCodec());
// ch.pipeline().addLast(new HttpObjectAggregator(64*1024));
// ch.pipeline().addLast(new WebSocketClientProtocolHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())));
// ch.pipeline().addLast(new WebSocketConnectedClientHandler(handler));
// }
// };
return result;
}
示例2: channelActive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
//logger.info("connected from {}", NetUtils.channelToString(channel.remoteAddress(), channel.localAddress()));
BaseServerHandler.addChannel(channel);
if (connectListeners != null) {
serverHandler.getBizThreadPool().execute(new Runnable() {
@Override
public void run() {
for (ConnectListener connectListener : connectListeners) {
try {
connectListener.connected(ctx);
} catch (Exception e) {
logger.warn("Failed to call connect listener when channel active", e);
}
}
}
});
}
}
示例3: channelActive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
this.channel = ctx.channel();
UnknownPandaServer.getLogger().info("Channel active: " + this.channel.remoteAddress());
}
示例4: send
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
/**
* Send back the result
* @param ctx channel context
* @param result rpc result
*/
private void send(ChannelHandlerContext ctx, Object result) {
int seqId = 0;
Channel ch = ctx.channel();
try {
seqId = ((ByteBuf) result).readInt();
((ByteBuf) result).resetReaderIndex();
AtomicBoolean channelInUse = channelStates.get(ctx);
if (channelInUse == null) {
return;
}
long startTs = System.currentTimeMillis();
while (true) {
if (channelInUse.compareAndSet(false, true)) {
ctx.writeAndFlush(result);
channelInUse.set(false);
LOG.debug(
"send response buf=" + result + ",channel ctx=" + ctx.channel() + ", seqId=" + seqId
+ " use time=" + (System.currentTimeMillis() - startTs));
return;
}
Thread.sleep(10);
}
} catch (Throwable ex) {
LOG.error("send response of request failed, request seqId=" + seqId + ", channel=" + ch, ex);
}
}
示例5: channelInactive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public final void channelInactive(ChannelHandlerContext ctx)throws Exception
{
Channel channel=ctx.channel();
JConnection connection = findConnection(channel);
if (connection != null)
{
listener.connectionClosed(connection);
} else
{
log.error(ctx.channel() + ":not found NettyConnection Created.");
}
super.channelInactive(ctx);
}
示例6: disconnected
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
public void disconnected(ChannelHandlerContext ctx)
{
JSON.ClientDisconnectMessage dmsg = new JSON.ClientDisconnectMessage( ctx.channel( ));
try {
outQ.put( dmsg);
}
catch (InterruptedException e) {
logr.info( "StockTickerMessageHandler.disconnected: outQ.put( ) failed for "
+ ctx + "\n" + e.toString( ));
}
}
示例7: call
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void call(ChannelHandlerContext ctx, MultiMessageToDeviceReq message) {
//找寻到对应设备的channel 将消息全部推送给这个设备
if (message != null) {
pushSender.send(message.getDevice(), PushReq.builder().msgs(message.getMessages()).build());
Channel channel = ctx.channel();
channel.writeAndFlush(MultiMessageToDeviceResp.builder().result(NodeMessageEnum.OK.getCode()).build().encode());
}
}
示例8: channelActive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception
{
super.channelActive(ctx);
this.channel = ctx.channel();
this.address = this.channel.remoteAddress();
//Try to handshake?
}
示例9: channelActive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if(initMessage != null){
ctx.writeAndFlush(initMessage);
} else {
logger.info("build connection success.");
channel[0] = ctx.channel();
ProcessData.Builder builder = ProcessData.newBuilder();
builder.setType(MessageType.HEART_BEAT_REQ.getValue());
ctx.fireChannelRead(builder.build());
}
}
示例10: channelInactive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
LOGGER.info("Disconnected telnet from {}", NetUtils.channelToString(channel.remoteAddress(), channel.localAddress()));
BaseServerHandler.removeChannel(channel);
charsetMap.remove(channel);
ALLOW_INVOKE_CHANNELS.remove(channel);
}
示例11: processSubscribe
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
private void processSubscribe(ChannelHandlerContext ctx, MqttSubscribeMessage mqttMsg) {
if (!checkConnected(ctx)) {
return;
}
log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
List<Integer> grantedQoSList = new ArrayList<>();
for (MqttTopicSubscription subscription : mqttMsg.payload().topicSubscriptions()) {
String topicName = subscription.topicName();
// TODO: handle this qos level.
MqttQoS reqQoS = subscription.qualityOfService();
try {
if (topicName.equals(DEVICE_ATTRIBUTES_TOPIC)) {
// AdaptorToSessionActorMsg msg =
// adaptor.convertToActorMsg(deviceSessionCtx,
// SUBSCRIBE_ATTRIBUTES_REQUEST,
// mqttMsg);
// BasicToDeviceActorSessionMsg basicToDeviceActorSessionMsg = new
// BasicToDeviceActorSessionMsg(
// deviceSessionCtx.getDevice(), msg);
// processor.process(basicToDeviceActorSessionMsg);
grantedQoSList.add(getMinSupportedQos(reqQoS));
} else if (topicName.equals(DEVICE_RPC_REQUESTS_SUB_TOPIC)) {
// AdaptorToSessionActorMsg msg =
// adaptor.convertToActorMsg(deviceSessionCtx,
// SUBSCRIBE_RPC_COMMANDS_REQUEST,
// mqttMsg);
// processor.process(new
// BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
grantedQoSList.add(getMinSupportedQos(reqQoS));
} else if (topicName.equals(DEVICE_RPC_RESPONSE_SUB_TOPIC)) {
grantedQoSList.add(getMinSupportedQos(reqQoS));
} else if (topicName.equals(DEVICE_ATTRIBUTES_RESPONSES_TOPIC)) {
deviceSessionCtx.setAllowAttributeResponses();
grantedQoSList.add(getMinSupportedQos(reqQoS));
} else if (topicName.equals(DEVICE_TELEMETRY_TOPIC)) {
grantedQoSList.add(getMinSupportedQos(reqQoS));
} else {
log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topicName, reqQoS);
grantedQoSList.add(FAILURE.value());
}
ChannelEntity channelEntity = new TcpChannelEntity(ctx.channel());
MemoryMetaPool.registerTopic(channelEntity, topicName);
} catch (Exception e) {
e.printStackTrace();
log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topicName, reqQoS);
grantedQoSList.add(FAILURE.value());
}
}
ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList));
}
示例12: openSession
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
public CLSession openSession(String identifier, ChannelHandlerContext ctx) {
CLSession session = new CLSession(this, identifier, (SocketChannel) ctx.channel());
this.players.put(identifier, session);
return session;
}
示例13: NettyConnection
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
public NettyConnection(ChannelHandlerContext ctx) {
this.ctx=ctx;
this.channel=ctx.channel();
this.id=getChannelId(channel);
channel.attr(CHANNEL_KEY).set(this);
}
示例14: handlerAdded
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void handlerAdded(final ChannelHandlerContext ctx) {
final Channel incoming = ctx.channel();
LOGGER.info("Handling connection");
channels.add(incoming);
}
示例15: channelActive
import io.netty.channel.ChannelHandlerContext; //导入方法依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
UkcpChannel kcpCh = (UkcpChannel) ctx.channel();
kcpCh.conv(EchoServer.CONV);
}