代码
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.plugins.mvc.websocket.AbstractWsEndpoint;
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import javax.websocket.EndpointConfig;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/websocket/ws", configurator = NutWsConfigurator.class)
@IocBean
public class MyWebsocket extends AbstractWsEndpoint {
final Log log = Logs.get();
@Inject
Dao dao;
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
super.onOpen(session, config);
log.infof("onOpen=%s", Json.toJson(config));
}
}
错误日志
[DEBUG] 17:12:34.289 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:712) - Processing socket [org.apache.tomcat.util.net.NioChannel@7cf0e502:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8090 remote=/0:0:0:0:0:0:0:1:58020]] with status [OPEN_READ]
[DEBUG] 17:12:34.289 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:724) - Found processor [null] for socket [org.apache.tomcat.util.net.NioChannel@7cf0e502:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8090 remote=/0:0:0:0:0:0:0:1:58020]]
[DEBUG] 17:12:34.289 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789) - Popped processor [org.apache.coyote.http11.Http11Processor@52335414] from cache
[DEBUG] 17:12:34.289 org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:402) - Received [GET /websocket/ws HTTP/1.1
Host: localhost:8090
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400
Upgrade: websocket
Origin: http://www.blue-zero.com
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Sec-WebSocket-Key: DioqoqK9YPHOGFtnxF8KTQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
]
[DEBUG] 17:12:34.290 org.apache.shiro.session.mgt.DefaultSessionManager.retrieveSession(DefaultSessionManager.java:218) - Unable to resolve session ID from SessionKey [org.apache.shiro.web.session.mgt.WebSessionKey@13e33f8d]. Returning null to indicate a session could not be found.
[DEBUG] 17:12:34.290 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) - Search mapping for [GET] path=/websocket/ws : NOT Action match
[DEBUG] 17:12:34.292 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:713) - Error parsing HTTP request header
java.io.EOFException
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1289)
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1223)
at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:729)
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:368)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:684)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
[DEBUG] 17:12:34.292 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:74) - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@38ecbc3e:org.apache.tomcat.util.net.NioChannel@7cf0e502:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8090 remote=/0:0:0:0:0:0:0:1:58020]], Status in: [OPEN_READ], State out: [CLOSED]
[DEBUG] 17:12:34.292 org.apache.coyote.AbstractProtocol$ConnectionHandler.release(AbstractProtocol.java:996) - Pushed Processor [org.apache.coyote.http11.Http11Processor@52335414]
[DEBUG] 17:12:41.292 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:712) - Processing socket [org.apache.tomcat.util.net.NioChannel@639246d6:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:57694]] with status [ERROR]
[DEBUG] 17:12:41.292 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:724) - Found processor [null] for socket [org.apache.tomcat.util.net.NioChannel@639246d6:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:57694]]
[DEBUG] 17:12:41.292 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:712) - Processing socket [org.apache.tomcat.util.net.NioChannel@3f0c3dc7:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:57773]] with status [ERROR]
[DEBUG] 17:12:41.292 org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:724) - Found processor [null] for socket [org.apache.tomcat.util.net.NioChannel@3f0c3dc7:java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:57773]]
[DEBUG] 17:12:48.498 org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:291) - batch acquisition of 1 triggers
[DEBUG] 17:13:00.000 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) - Get 'helloQuartz'<class com.liangyufeng.nutz.quartz.HelloQuartz>
[DEBUG] 17:13:00.000 org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:291) - batch acquisition of 0 triggers
[DEBUG] 17:13:00.000 org.quartz.core.JobRunShell.run(JobRunShell.java:201) - Calling execute on job DEFAULT.com.liangyufeng.nutz.quartz.HelloQuartz
[INFO ] 17:13:00.000 com.liangyufeng.nutz.quartz.HelloQuartz.execute(HelloQuartz.java:18) - time=1560157980000
前端代码
initWebSocket(){ //初始化weosocket
const wsuri = "ws://localhost:8090/websocket/ws";//ws地址
this.websock = new WebSocket(wsuri);
this.websocket.onopen = this.websocketonopen;
this.websocket.onerror = this.websocketonerror;
this.websock.onmessage = this.websocketonmessage;
this.websock.onclose = this.websocketclose;
},
websocketonopen() {
console.log("WebSocket连接成功");
},
websocketonerror(e) { //错误
console.log("WebSocket连接发生错误");
},
websocketonmessage(e){ //数据接收
const redata = JSON.parse(e.data);
//注意:长连接我们是后台直接1秒推送一条数据,
//但是点击某个列表时,会发送给后台一个标识,后台根据此标识返回相对应的数据,
//这个时候数据就只能从一个出口出,所以让后台加了一个键,例如键为1时,是每隔1秒推送的数据,为2时是发送标识后再推送的数据,以作区分
console.log(redata.value);
},
websocketsend(agentData){//数据发送
this.websock.send(agentData);
},
websocketclose(e){ //关闭
console.log("connection closed (" + e.code + ")");
},