1.tomcat jetty websocketå¯ä»¥å
¬ç¨å
tomcat jetty websocketå¯ä»¥å ¬ç¨å
Tomcatï¼
J2EEä¸é¢ç¨çæå¤ç容å¨åºè¯¥å°±æ¯tomcatäºã说å°tomcat对WebSocketçæ¯æï¼ä¸å¾ä¸å æä¸ä¸ï¼ç®åçWebSocketå议已ç»ç»è¿äºå¥½å 代çæ¼åï¼ä¸åæµè§å¨å¯¹æ¤åè®®çæ¯æç¨åº¦ä¹ä¸åï¼å æ¤ï¼å¦æä½ä¸ºæå¡å¨ï¼æçæ³çæ¯æ¯æå°½å¯è½å¤çWebSocketåè®®çæ¬ã
tomcat8çæ£æ¯æjsr-ï¼å å«å¯¹websocketçæ¯æï¼ï¼ tomcat7æ¯æé¨åçæ¬çwebsocketå®ç°ä¸å ¼å®¹jsr-ãå æ¤ï¼è½ç¨tomcat8çè¯ï¼è¿æ¯å°½éç¨ã
代ç å®ç°ç¸å½ç®åï¼ä»¥ä¸æ¯ä¸ä¸ªååï¼åªéè¦tomcat8çåºæ¬åºï¼ä¸éè¦å ¶ä»ä¾èµã
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocketTest {
@OnMessage
public void onMessage(String message,flash在线预览源码 Session session) throws IOException,
InterruptedException {
// Print the client message for testing purposes
System.out.println("Received: " + message);
// Send the first message to the client
session.getBasicRemote().sendText("This is the first server message");
// Send 3 messages to the client every 5 seconds
int sentMessages = 0;
while (sentMessages < 3) {
Thread.sleep();
session.getBasicRemote().sendText("This is an intermediate server message. Count: " + sentMessages);
sentMessages++;
}
// Send a final message to the client
session.getBasicRemote().sendText("This is the last server message");
}
@OnOpen
public void onOpen() {
System.out.println("Client connected");
}
@OnClose
public void onClose() {
System.out.println("Connection closed");
}
}