2015-06-16 41 views
0

我有以下ServerEndpoint这只不过是测试更多:无法打开Tomcat上与WebSocket连接8

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(value = "/echo") 
public class EchoService { 

/** 
* @OnOpen allows us to intercept the creation of a new session. 
* The session class allows us to send data to the user. 
* In the method onOpen, we'll let the user know that the handshake was 
* successful. 
*/ 
@OnOpen 
public void onOpen(Session session){ 
    System.out.println(session.getId() + " has opened a connection"); 
    try { 
     session.getBasicRemote().sendText("Connection Established"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

/** 
* When a user sends a message to the server, this method will intercept the message 
* and allow us to react to it. For now the message is read as a String. 
*/ 
@OnMessage 
public void onMessage(String message, Session session){ 
    System.out.println("Message from " + session.getId() + ": " + message); 
    try { 
     session.getBasicRemote().sendText(message); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

/** 
* The user closes the connection. 
* 
* Note: you can't send messages to the client from this method 
*/ 
@OnClose 
public void onClose(Session session){ 
    System.out.println("Session " +session.getId()+" has ended"); 
} 

} 

现在我想将它连接使用Simple Websocket Client Chrome Extension,但它从来没有连接。

我认为这可能与我使用Tomcat 8作为servlet容器的事实有关。我加入这个依赖于我的pom.xml

<dependency> 
    <groupId>javax.websocket</groupId> 
    <artifactId>javax.websocket-api</artifactId> 
    <version>1.0</version> 
    <scope>provided</scope> 
</dependency> 

注意,我设置好的,因为它的tomcat 8自带的WebSocket API所提供.. 我想知道如果我要补充点东西给我的web.xml,但无法找到任何有关..

的URI,我试图连接是WS://本地主机:8080 /电子商务画面/回音,因为我的项目战争被命名为电子商务视

任何帮助是值得欢迎的。

回答

0

试用@ServerEndpoint("/echo")。所以,删除value=

+0

没有工作:( –

+0

嗯..我试图复制你的项目与相同的EchoService,相同的依赖和相同的铬插件和一切工作正常。因此,代码明智,它似乎很好。你的EchoService在src/main/java目录下吗?在tomcat日志中有什么奇怪的东西? – sarumait

+0

是的,没有,没有记录,也许我的问题是web.xml上的问题,我将在明天更新这个问题的文件内容 –