我在使用Java连接到websocket时遇到问题。我在以前在localhost端口8080上运行服务器端点时,我的代码工作正常,但是当我尝试连接到我在网上找到的websocket时,我正在
javax.websocket.DeploymentException:来自服务器的HTTP响应[HTTP / 1.1 200 OK]不允许HTTP升级到WebSocket
我使用Simple Websocket客户端chrome扩展测试了ws端点,我得到了响应。所以服务器肯定能正常工作。
我的客户代码:
package clientendpoints;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
//import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;
@ClientEndpoint
public class MyClientEndpoint {
private Logger logger = Logger.getLogger(this.getClass().getName());
Session session = null;
public MyClientEndpoint(String endpointURI) {
//start connection
javax.websocket.WebSocketContainer container = javax.websocket.ContainerProvider.getWebSocketContainer();
try {
container.connectToServer(this, new URI(endpointURI));
} catch (Exception e) {
e.printStackTrace();
}
}
@OnOpen
public void onOpen(Session session) {
logger.info("Client onOpen..." + session.getId());
this.session = session;
}
@OnMessage
public void onMessage(String message, Session session) {
logger.info("MyClientEndpoint log: " + message);
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s close because of %s", session.getId(), closeReason));
}
我用
来称呼它MyClientEndpoint client = new MyClientEndpoint(MY_SECRET_ENDPOINT);