Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用nodejs连接apachemina服务器?_Java_Node.js_Sockets_Tcp_Mina - Fatal编程技术网

Java 如何使用nodejs连接apachemina服务器?

Java 如何使用nodejs连接apachemina服务器?,java,node.js,sockets,tcp,mina,Java,Node.js,Sockets,Tcp,Mina,上面用于连接mina服务器的代码: 现在,我想用nodejs来做。我只在网上找到这个 我尝试了frame stream、net.Socket,但没有对('data')事件触发。我在这里使用演示代码,它可以工作: 发现我错过了客户端中的\r\n;将消息推送到mina服务器 package com.buscontrol.site.message; import java.net.InetSocketAddress; import java.nio.charset.Charset; import

上面用于连接mina服务器的代码:

现在,我想用nodejs来做。我只在网上找到这个


我尝试了
frame stream
net.Socket
,但没有对('data')事件触发

我在这里使用演示代码,它可以工作:

发现我错过了
客户端中的
\r\n
;将消息推送到mina服务器

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("请求消息格式不正确,缺少请求参数");
        }
    }
}