020-02-24 16:09:20 INFO [main] org.yeauty.standard.ServerEndpointExporter 77 - Netty WebSocket started on port: 8089 with context path(s): '/ws/{userId}'
2020-02-24 16:09:22 INFO [main] org.yeauty.standard.ServerEndpointExporter 77 - Netty WebSocket started on port: 8089 with context path(s): '/ws/{userId}'
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:134)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:550)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)``
启动会打印2边启动端口,请大佬指教下
后端代码:
@ServerEndpoint(port = "${ws.port}",path = "/ws/{userId}")
@Slf4j
public class MyWebSocket {
}
020-02-24 16:09:20 INFO [main] org.yeauty.standard.ServerEndpointExporter 77 - Netty WebSocket started on port: 8089 with context path(s): '/ws/{userId}'
2020-02-24 16:09:22 INFO [main] org.yeauty.standard.ServerEndpointExporter 77 - Netty WebSocket started on port: 8089 with context path(s): '/ws/{userId}'
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:134)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:550)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)``
启动会打印2边启动端口,请大佬指教下
后端代码:
@ServerEndpoint(port = "${ws.port}",path = "/ws/{userId}")
@slf4j
public class MyWebSocket {
}
测了下,没遇到类似的问题。
有demo完整代码吗?(包括application.properties)
我多加了一个这个类ServerEndpointExporter,才导致会加载2边
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
我多加了一个这个类ServerEndpointExporter,才导致会加载2边
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
把这个去掉
在MyWebSocket 类中,不能有构造函数吗
代码
`@ServerEndpoint(port="${ws.port}",path = "/ws/{userId}")
@Slf4j
public class MyWebSocket {
public static final Map<String, Session> sessionMap;
public static final Map<String, Channal> interfaceMap;
@Autowired
private VideoBusiness videoBusiness;
static {
sessionMap = new ConcurrentHashMap<String, Session>();
interfaceMap = new ConcurrentHashMap<String, Channal>();
}
public MyWebSocket(List<Channal> channals){
for (Channal channal : channals) {
interfaceMap.put(channal.type(),channal);
}
}
@BeforeHandshake
public void handshake(Session session, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String userId, @PathVariable Map pathMap){
session.setSubprotocols("stomp");
if (!req.equals("ok")){
log.info("Authentication failed!");
session.close();
}
}
@OnOpen
public void onOpen(Session session, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String userId, @PathVariable Map pathMap){
session.setAttribute("userId", userId);
if (!sessionMap.containsKey(userId)) {
sessionMap.put(userId,session);
}
log.info("new connection,在线人数:{}", sessionMap.size());
}
@OnClose
public void onClose(Session session,@PathVariable String userId) throws IOException {
sessionMap.remove(userId);
log.info("one connection closed,userId: {},在线人数:{}",userId,sessionMap.size());
}
@OnError
public void onError(Session session, Throwable throwable) {
throwable.printStackTrace();
}
@OnMessage
public void onMessage(Session session, String message) {
log.info("收到消息:{}",message);
String msg = "Hello Netty!";
if ("0".equals(message)) {
msg = "heartBeat: "+LocalDateTime.now().toString();
}
msg = interfaceMap.get("video").handleMethod(session).toString();
session.sendText(msg);
}
@OnBinary
public void onBinary(Session session, byte[] bytes) {
for (byte b : bytes) {
System.out.println(b);
}
session.sendBinary(bytes);
}
@OnEvent
public void onEvent(Session session, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
switch (idleStateEvent.state()) {
case READER_IDLE:
System.out.println("read idle");
break;
case WRITER_IDLE:
System.out.println("write idle");
break;
case ALL_IDLE:
System.out.println("all idle");
break;
default:
break;
}
}
}
}
java.lang.NoSuchMethodException: com.mti.websocket.config.MyWebSocket.()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.yeauty.pojo.PojoMethodMapping.getEndpointInstance(PojoMethodMapping.java:248)
at org.yeauty.pojo.PojoEndpointServer.doBeforeHandshake(PojoEndpointServer.java:63)
at org.yeauty.standard.HttpServerHandler.handleHttpRequest(HttpServerHandler.java:204)
at org.yeauty.standard.HttpServerHandler.channelRead0(HttpServerHandler.java:83)
at org.yeauty.standard.HttpServerHandler.channelRead0(HttpServerHandler.java:28)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:295)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
2020-02-24 22:48:54 ERROR [nioEventLoopGroup-3-1] org.yeauty.pojo.PojoEndpointServer 89 - Unexpected exception:
java.lang.NullPointerException: null
at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.yeauty.pojo.PojoEndpointServer.doOnOpen(PojoEndpointServer.java:90)
at org.yeauty.standard.HttpServerHandler.lambda$handleHttpRequest$0(HttpServerHandler.java:233)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:577)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:551)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:490)
at io.netty.util.concurrent.DefaultPromise.addListener(DefaultPromise.java:183)
at io.netty.channel.DefaultChannelPromise.addListener(DefaultChannelPromise.java:95)
at io.netty.channel.DefaultChannelPromise.addListener(DefaultChannelPromise.java:30)
at org.yeauty.standard.HttpServerHandler.handleHttpRequest(HttpServerHandler.java:231)
at org.yeauty.standard.HttpServerHandler.channelRead0(HttpServerHandler.java:83)
at org.yeauty.standard.HttpServerHandler.channelRead0(HttpServerHandler.java:28)
Disconnected from the target VM, address: '127.0.0.1:55296', transport: 'socket'
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:295)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)`
我加上无参构造没有报错。但是我想让让类加载时,就加载这个有参构造,有没有什么好的方法,求指教一下,谢谢
我加上无参构造没有报错。但是我想让让类加载时,就加载这个有参构造,有没有什么好的方法,求指教一下,谢谢
Lis<Channal> channals是你自己传进来的吗?
@Autowired
private VideoBusiness videoBusiness;
public MyWebSocket(List<Channal> channals){
for (Channal channal : channals) {
interfaceMap.put(channal.type(),channal);
}
}
Channal是个自己写的接口,这个有参构造是自动的加载的,自己传进来的,VideoBusiness 这个类实现了Channal接口,这个有参构造相当于会把实现Channal接口的类全部放到map中,这个作用
Channal是个自己写的接口,这个有参构造是自动的加载的,自己传进来的,VideoBusiness 这个类实现了Channal接口,这个有参构造相当于会把实现Channal接口的类全部放到map中,这个作用
channals从哪里来的。即使是自己的接口,实现类在哪。
如果对象不是你创建的,那么你就没办法使用有参构造。
channals不用自己手动传进去,因为VideoBusiness类上面加了@Component注解,交给spring管理的,所以有参构造会自动加载
channals不用自己手动传进去,因为VideoBusiness类上面加了@component注解,交给spring管理的,所以有参构造会自动加载
这个类的对象既不是spring生成,也不交给spring管理,所以没办法这么做。
只能自己想办法实现,如:无参构造通过调用某个静态方法将这个List设置进来