我试图从一个 Java 客户端连接到我的 nodejs sockjs 服务器。
sockjs 服务器只是一个简单的 echo 服务器,取自 git 上的示例:
var http = require('http');
var sockjs = require('sockjs');
var node_static = require('node-static');
// 1. Echo sockjs server
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};
var sockjs_echo = sockjs.createServer(sockjs_opts);
sockjs_echo.on('connection', function(conn) {
conn.on('data', function(message) {
conn.write(message);
});
});
// 2. Static files server
var static_directory = new node_static.Server(__dirname);
// 3. Usual http stuff
var server = http.createServer();
server.addListener('request', function(req, res) {
static_directory.serve(req, res);
});
server.addListener('upgrade', function(req,res){
res.end();
});
sockjs_echo.installHandlers(server, {prefix:'/echo'});
console.log(' [*] Listening on 0.0.0.0:8080' );
server.listen(8080, '0.0.0.0');
现在我尝试从我的 Java 客户端连接以下内容:
public static void connect() throws Exception {
final WebSocket ws = new WebSocket();
ws.addWebSocketListener(
new WebSocketAdapter() {
@Override
public void onMessage(WebSocketEvent messageEvent) {
System.out.println("Received Event Data: " + messageEvent.getData());
// let's close the open connection...
try {
ws.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onOpen(WebSocketEvent openEvent) {
System.out.println("Connection to Server is up!");
// we are able to talk to the WebSocket gateway
try {
ws.send("Hey, server!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
);
ws.connect(new URI("ws://server.hello.com:8080/echo/websocket"));
}
我的 java 客户端尝试连接时遇到的错误是:
com.kaazing.gateway.client.html5.impl.bridge.BridgeUtil initCrossOriginProxy 警告:无法连接:网关可能未运行,网络路由可能不可用,或网关配置不正确
有什么想法可能是错的吗?