2017-02-15 83 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("請求消息格式不正確,缺少請求參數"); 
     } 
    } 
} 

上面的代碼用於連接服務器米娜:如何連接apache mina服務器使用nodejs?

現在,我想這樣做的NodeJS它。我只能在網上找到這個。

Sending message to MINA socket server from Node JS Socket client

我試過frame-streamnet.Socket,但沒有on('data')觸發的事件。

回答

相關問題