猿问

netty-socketio 客户端刷新后台报错

netty-socketio 客户端刷新后台报错

我使用的是这个项目:https://github.com/mrniko/net...

这个项目前后端都没有问题,但是我将这个项目的后端开启,并将其前端代码复制到我自己的项目的jsp页面中,然后启动我的项目进入jsp页面,是可以正常连接和发送消息,但是会在页面刷新的时候就会报如下错误

[nioEventLoopGroup-3-3] ERROR com.corundumstudio.socketio.listener.DefaultExceptionListener - 远程主机强迫关闭了一个现有的连接。
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:367)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:118)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:610)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:551)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:465)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:437)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
    at java.lang.Thread.run(Thread.java:745)

这个问题我找了下,觉得有如下解决办法,但是实在没有找到正确的实现方式:
1.我用的是netty-socketio-1.7.12这个jar包实现的socket.io的server,所以出现上述错误,我认为可以用netty框架中的抓取所有错误的拦截器来处理这个错误的情况,这个jar包中带的netty是4.1.5.Final版本。
2.jsp项目中有什么办法可以使js中的socket连接设置为全局变量,让其不管在哪里都可以保持连接
3.其他我所不知道的方法,请求大佬指点。。。。

如果上述方式没有办法实现,最后一个问题也是我最终目的:就是在java web jsp页面中将前端ajax轮询修改为后端推送方式的方法的想法,至少要适应ie10版本

MMMHUHU
浏览 1237回答 1
1回答

饮歌长啸

已经找到方法了,代码如下: Configuration config = new Configuration(); config.setHostname("localhost"); config.setPort(9092); config.setExceptionListener(new MyExceptionListener()); import io.netty.channel.ChannelHandlerContext; import com.corundumstudio.socketio.listener.ExceptionListenerAdapter; public class MyExceptionListener extends ExceptionListenerAdapter{ @Override public boolean exceptionCaught(ChannelHandlerContext ctx, Throwable e) { System.out.println(e.getMessage()); ctx.close(); return true; } } exceptionCaught方法中返回true才是不抛出异常
随时随地看视频慕课网APP

相关分类

Java
我要回答