从Node JS Socket客户端

时间:2016-09-10 17:30:09

标签: node.js sockets mina

我有一个如下的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.access$1200(DefaultIoFilterChain.java:47)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.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.access$1200(DefaultIoFilterChain.java:47)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.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.access$600(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模块,但没有运气。

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

1 个答案:

答案 0 :(得分:0)

使用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);   
});