Sending message to MINA socket server from Node JS Socket client

橙三吉。 提交于 2019-12-25 09:14:53

问题


I have a MENA server as follows which uses 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) );

And the node client which connects to this server and writes the message looks like below.

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

Which results in the following exception in the MINA server.

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)

I also tried using length-prefixed-message module but no luck.

Any help on how to send the message in the correct format from node client would be helpful.


回答1:


Using frame-stream solved the issue.

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


来源:https://stackoverflow.com/questions/39428751/sending-message-to-mina-socket-server-from-node-js-socket-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!