Socket.IO连接服务

来源:互联网 发布:win2008端口转发 编辑:程序博客网 时间:2024/06/06 11:38

https://github.com/socketio/socket.io-client-java


publicstaticSocketsocket;   //共享该连接

       /**
        * 连接游戏服务
        *<p>Description: </p>
        *@throwsURISyntaxException
        */
       publicstaticvoidconnectService() throwsURISyntaxException{
             StringserviceAddress= GetPath.getServiceAddress();     //读取配置中的服务器地址
             socket= IO.socket(serviceAddress);
             socket.on(Socket.EVENT_DISCONNECT,newEmitter.Listener() {
                     @Override
                     publicvoidcall(Object...args) {
                             System.out.println("断开监听连接");
                      }
                    
                    });
             
             //连接 或者 重连服务器的时候   EVENT_CONNECT 连接事件  Socket.IO -Java 内部会自动重连服务
             socket.on(Socket.EVENT_CONNECT,newEmitter.Listener() {
                    @Override
                    publicvoidcall(Object...args) {
                           JSONObjectevents2=newJSONObject();
                           JSONObjectacctValue=newJSONObject();
                           acctValue.put("name","test");
                           acctValue.put("password","password");
                           events2.put("event","user.auth");
                           events2.put("args",acctValue);
                           socket.emit("events",events2);// 发送验证用户
                           
                           System.out.println("发送验证用户="+events2);
                           System.out.println("连接成功");
                    }
                    
             });
             
             socket.connect();
       }


原创粉丝点击