无题
SpringBoot中使用Servlet
在SpringBoot启动类添加@ServletComponentScan注解
问题:tomcat8.5会只返回200,不返回OK
解决办法:response.setStatus(HttpServletResponse.SC_OK,“OK”);
websocket java使用demo。包含客户端与服务端使用
客户端依赖:
如果使用上面的类库会出现下面异常
Exception in thread “main” java.lang.RuntimeException: Could not find an implementation class.
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
at connect.(connect.java:21)
at test.main(test.java:11)
客户端代码:
package com.xxg.websocket.client;
import java.io.IOException;
import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
@ClientEndpoint
public class WebSocketClient {
private Session session = null;
private int count = 0;
@OnOpen
public void onOpen(Session session){
sendMessage("onOpen hello benny onOpen");
}
@OnClose
public void onClose(){
}
@OnMessage
public void onMessage(String message, Session session){
System.out.println("server message:"+message);
if(count <10){
sendMessage("onMessage hello benny "+(++count));
}
}
@OnError
public void onError(Throwable thr){
}
public WebSocketClient() {
super();
}
public WebSocketClient(URI endpointURI) {
super();
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer(); // 获得WebSocketContainer
this.session = container.connectToServer(WebSocketClient.class, endpointURI); // 该方法会阻塞
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public void sendMessage(String message){
try {
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
this.session.getBasicRemote().flushBatch();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
上一篇: 穿越计算机迷雾