我正在尝试使用spring 4.0,jsf和glassfish 4.0构建一个简单的教学websocket应用程序 我已经创建了一个maven web项目(因为这个应用程序有另一个Web组件(jsf)),并且从这个应用程序我试图设置一些websockets。
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(echoHandler(), "/echo");
}
@Bean
public WebSocketHandler echoHandler() {
return new EchoHandler();
}
}
和
public class EchoHandler extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
session.sendMessage(message);
}
}
在客户端一个非常简单的连接:
<script>
/* <![CDATA[ */
var endPointURL = "ws://localhost:8080/liveasterisk/echo";
var chatClient = null;
function connect () {
chatClient = new WebSocket(endPointURL);
chatClient.onmessage = function (event) {
alert(event);
};
}
function disconnect () {
chatClient.close();
}
function sendMessage() {
chatClient.send("xxx");
}
connect();
/* ]]> */
</script>
问题是当connect()方法触发时,我得到404响应。
我想我必须以某种方式训练jsf来响应握手请求。 我的所有* .xhtml都映射到jsf servlet。
我已经解决了这个问题:
@ServerEndpoint(value = "/keepalive", configurator = SpringConfigurator.class)
public class KeepAliveEndpoint {
private static Logger logger = Logger.getLogger(KeepAliveEndpoint.class);
@Autowired
private KeepAliveService keepAliveService;
private List<Session> sessions = new ArrayList<Session>();
@OnOpen
public void onOpen(Session session) {
sessions.add(session);
System.out.println("onOpen: " + session.getId()+" list size: " + sessions.size());
}
@OnClose
public void onClose(Session session) {
System.out.println("onClose: " + session.getId());
sessions.remove(session);
}
@OnMessage
public void handleMessage(Session session, String message) {
try{
Long userId = Long.parseLong(message);
keepAliveService.keepAlive(userId);
}catch(NumberFormatException nfe){
try {
session.getBasicRemote().sendText("Cannot perform live update for your status");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
所以现在我通过jsf暴露了一个套接字,我可以在这个端点注入@Autowired的“服务”。
并使用此js代码:
<script type="text/javascript">
var host = "ws://localhost:8080/myApp/keepalive";
var wSocket = new WebSocket(host);
var browserSupport = ("WebSocket" in window) ? true : false;
// called body onLoad()
function initializeReception() {
if (browserSupport) {
wSocket.onopen = function(){
setInterval(function(){wSocket.send('<h:outputText value="#{managedBean.userDTO.id}" />')}, 300000);
};
// called when a message is received
wSocket.onmessage = function(event) {
alert(event.data);
};
// on error handler
wSocket.onError = function(event) {
alert('An error has occured '+event.data+'.');
};
// called when socket closes
wSocket.onclose = function(){
// websocket is closed.
//alert("Connection is closed...");
};
}
else {
// The browser doesn't support WebSocket
alert("WebSocket is NOT supported by your Browser!");
}
}
initializeReception();
</script>
答案 0 :(得分:0)
以上配置适用于Spring MVC的DispatcherServlet
。您是否在Web应用程序中配置了一个?根据servlet映射(上面未显示),您很可能需要在URL中添加一个部分以匹配servlet映射。
更长的解释是@EnableWebSocket
创建了一个HandlerMapping,它将“/ echo”映射到WebSocketHandler。 HandlerMapping需要驻留在DispatcherServlet的配置中,以便处理HTTP握手。