从 Node JS 套接字客户端向 MINA 套接字服务器发送消息

Sending message to MINA socket server from Node JS Socket client

我有一个 MENA 服务器,如下所示,它使用 PrefixedStringCodecFactory。

 IoAcceptor acceptor = new NioSocketAcceptor();

 acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
 acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new PrefixedStringCodecFactory( Charset.forName( "UTF-8" ))));

 acceptor.setHandler( new MinaServerHandler() );
 acceptor.getSessionConfig().setReadBufferSize( 2048 );
 acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
 acceptor.bind( new InetSocketAddress(PORT) );

连接到此服务器并写入消息的节点客户端如下所示。

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 9998;
var client = new net.Socket();
var intervalHandler = null;
client.connect(PORT, HOST, function(){
   console.log('CONNECTED TO: ' + HOST + ':' + PORT);
   var payload = "{\"message\": \"Hello\"}";
   client.write(payload);
});

这导致 MINA 服务器出现以下异常。

org.apache.mina.filter.codec.ProtocolDecoderException: org.apache.mina.core.buffer.BufferDataException: dataLength: 2065854578 (Hexdump: 7B 22 70 72 6F 63 65 73 73 49 64 22 3A 20 30 2C 20 22 63 6F 6D 6D 61 6E 64 22 3A 20 22 41 4C 49 56 45 22 2C 20 22 50 72 6F 63 65 73 73 4E 61 6D 65 22 3A 20 22 4B 75 62 65 41 70 70 73 22 7D)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:242)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:417)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:47)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.messageReceived(DefaultIoFilterChain.java:765)
at org.apache.mina.filter.logging.LoggingFilter.messageReceived(LoggingFilter.java:208)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:417)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access00(DefaultIoFilterChain.java:47)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl.messageReceived(DefaultIoFilterChain.java:765)
at org.apache.mina.core.filterchain.IoFilterAdapter.messageReceived(IoFilterAdapter.java:109)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:417)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.fireMessageReceived(DefaultIoFilterChain.java:410)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:710)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:664)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:653)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.access0(AbstractPollingIoProcessor.java:67)
at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1124)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我也试过使用 length-prefixed-message 模块,但没有成功。

任何有关如何从节点客户端以正确格式发送消息的帮助都会有所帮助。

使用 frame-stream 解决了问题。

var net = require('net');
var frame = require('frame-stream')
var HOST = '127.0.0.1';
var PORT = 9998;
var client = new net.Socket();
var socket = client.connect(PORT, HOST, function(){
   var payload = "{\"status\": 0}";
   var message = new Buffer(payload); 
   var encode = frame.encode(); 
   encode.pipe(socket)
        .pipe(frame());

   encode.write(message);   
});