![](/img/trans.png)
[英]How to connect to an another server from apache mina server's MessageRecieved API
[英]How to connect apache mina server use nodejs?
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("請求消息格式不正確,缺少請求參數");
}
}
}
上面的代碼用於連接Mina服務器:
現在,我想用nodejs來做。 我只能在互聯網上找到它。
從Node JS Socket客戶端向MINA套接字服務器發送消息
我嘗試了frame-stream
, net.Socket
,但是沒有觸發on('data')
事件。
我在這里使用演示代碼,並且可以正常工作: https : //nodejs.org/dist/latest-v7.x/docs/api/net.html#net_net_connect_options_connectlistener
發現,我錯過了\\r\\n
在client.write('{\\"sstation\\":\\"123\\"}\\r\\n')
; 將消息推送到Mina服務器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.