zoukankan      html  css  js  c++  java
  • Myfox开发笔记

    1.一个头疼的问题,客户端怎么组织随时发送消息,在ManWeb.connect()中通过Sendmessage.ch = f.channel();拿到channel然后保存到另一个类里面就好了,channel.writeandflush可以写消息。(参见channel官方文档)

    public class ManWeb {
        public static int serverport = 18872;
        public static String serverip = "47.106.92.228";
    
        public static void connect()throws Exception{
            // 配置NIO线程组
            EventLoopGroup group = new NioEventLoopGroup();
    
            try {
                // Bootstrap 类,是启动NIO服务器的辅助启动类
                Bootstrap b = new Bootstrap();
                b.group(group).channel(NioSocketChannel.class)
                        .option(ChannelOption.TCP_NODELAY,true)
                        .handler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch)
                                    throws Exception{
                                //
                                ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
    
                                ch.pipeline().addLast(new ProtobufDecoder(MessageProbuf.Message.getDefaultInstance()));
                                ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                                ch.pipeline().addLast(new ProtobufEncoder());
                                ch.pipeline().addLast(new Clienthandler());
    
                            }
                        });
    
                // 发起异步连接操作
                ChannelFuture f= b.connect(serverport,serverip).sync();
    
                Sendmessage.ch = f.channel();
    
                // 等待客服端链路关闭
                f.channel().closeFuture().sync();
            }finally {
                group.shutdownGracefully();
            }
        }
    
    }
    public class Sendmessage{
        public static channel ch;
    
        //Clienthandler.channalActive use senderid to notify server online
        public static int senderid;
        public static String sendername;
    
        public static void send(MessageProbuf.Message mess){
            ch.writeAndFlush(mess);
        }
    }
  • 相关阅读:
    go语言切片
    sharding-jdbc分库分表配置,多数据源
    spring boot的配置文件
    go-micro生成项目
    自定义注解+aop实现jetcache功能扩展
    linux下mysql忘记密码解决方案
    MySQL 1130错误,无法远程连接
    Linux/UNIX 上安装 MySQL
    BarTender遇到的问题
    SourceTree安装使用
  • 原文地址:https://www.cnblogs.com/CreatorKou/p/9191783.html
Copyright © 2011-2022 走看看