2017-02-15 2 views
0
package com.buscontrol.site.message; 

import java.net.InetSocketAddress; 
import java.nio.charset.Charset; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 
import org.apache.mina.core.RuntimeIoException; 
import org.apache.mina.core.future.ConnectFuture; 
import org.apache.mina.core.service.IoHandlerAdapter; 
import org.apache.mina.core.session.IdleStatus; 
import org.apache.mina.core.session.IoSession; 
import org.apache.mina.filter.codec.ProtocolCodecFilter; 
import org.apache.mina.filter.codec.textline.TextLineCodecFactory; 
import org.apache.mina.transport.socket.SocketConnector; 
import org.apache.mina.transport.socket.SocketSessionConfig; 
import org.apache.mina.transport.socket.nio.NioSocketConnector; 

public class Client extends IoHandlerAdapter { 
    public static final int CONNECT_TIMEOUT = 30000; 
    private String host; 
    private int port; 
    private SocketConnector connector; 
    private IoSession session; 
    public Object responsecontent = null; 
    boolean retuanflag; 

    public Object getResponsecontent() { 
     return this.responsecontent; 
    } 

    public void setResponsecontent(Object responsecontent) { 
     this.responsecontent = responsecontent; 
    } 

    public boolean isRetuanflag() { 
     return this.retuanflag; 
    } 

    public void setRetuanflag(boolean retuanflag) { 
     this.retuanflag = retuanflag; 
    } 

    public Client(String host, int port) { 
     this.host = host; 
     this.port = port; 
     this.connector = new NioSocketConnector(); 
     TextLineCodecFactory lineCodec = new TextLineCodecFactory(Charset.forName("UTF-8")); 
     lineCodec.setDecoderMaxLineLength(1048576); 
     lineCodec.setEncoderMaxLineLength(1048576); 
     this.connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(lineCodec)); 
     this.connector.setHandler(this); 
    } 

    public boolean isConnected() { 
     return this.session != null && this.session.isConnected(); 
    } 

    public void connect() throws Exception { 
     ConnectFuture connectFuture = this.connector.connect(new InetSocketAddress(this.host, this.port)); 
     connectFuture.awaitUninterruptibly(30000L); 

     try { 
      this.session = connectFuture.getSession(); 
      if(this.session == null) { 
       throw new Exception("连接服务器失败"); 
      } 
     } catch (RuntimeIoException var3) { 
      this.connector.dispose(); 
      throw new Exception(var3.getMessage()); 
     } 
    } 

    public void disconnect() { 
     if(this.session != null) { 
      this.session.close(true).awaitUninterruptibly(30000L); 
      this.session = null; 
      this.connector.dispose(); 
     } 

    } 

    public void sessionOpened(IoSession session) throws Exception { 
    } 

    public void sessionClosed(IoSession session) throws Exception { 
    } 

    public void sessionIdle(IoSession session, IdleStatus status) throws Exception { 
     session.close(true); 
    } 

    public void sessionCreated(IoSession session) throws Exception { 
     super.sessionCreated(session); 
     SocketSessionConfig cfg = (SocketSessionConfig)session.getConfig(); 
     cfg.setReceiveBufferSize(2097152); 
     cfg.setReadBufferSize(2097152); 
     cfg.setKeepAlive(true); 
     cfg.setSoLinger(0); 
    } 

    public void sendRequest(String request) throws Exception { 
     if(this.session == null) { 
      throw new Exception("没有连接,请重新登录连接服务器"); 
     } else { 
      this.session.write(request); 
     } 
    } 

    public void messageReceived(IoSession session, Object message) throws Exception { 
     String response = (String)message; 
     JSONObject ja = null; 

     try { 
      ja = (JSONObject)JSONSerializer.toJSON(message); 
      this.responsecontent = ja; 
     } catch (Exception var6) { 
      this.responsecontent = null; 
     } 

     this.retuanflag = true; 
    } 

    public void exceptionCaught(IoSession session, Throwable cause) throws Exception { 
     session.close(true); 
     System.out.println(cause.getMessage()); 
    } 

    public static void main(String[] args) throws Exception { 
     Client c = new Client("localhost", 8698); 
     c.connect(); 
     c.sendRequest("连接测试1"); 

     try { 
      Thread.sleep(2000L); 
     } catch (InterruptedException var3) { 
      var3.printStackTrace(); 
     } 

    } 

    private Object getResponseObject(JSONObject ja) throws Exception { 
     Object response = null; 

     try { 
      if(ja != null) { 
       response = ja.get("result"); 
       if(response == null) { 
        throw new Exception(); 
       } else { 
        return response; 
       } 
      } else { 
       throw new Exception(); 
      } 
     } catch (Exception var4) { 
      throw new Exception("请求消息格式不正确,缺少请求参数"); 
     } 
    } 
} 

Der obige Code verwendet mina-Server verbinden:Wie verbinde ich Apache Mina Server mit Nodejs?

Jetzt möchte ich es mit NodeJS tun. Ich finde das nur im Internet.

Sending message to MINA socket server from Node JS Socket client

Ich versuchte frame-stream, net.Socket, aber kein on('data') Ereignis ausgelöst.

Antwort

Verwandte Themen