我是网络套接字编程的新手......
我有以下JavaScript客户端代码:
var connection = new WebSocket('ws://localhost:8080/OmegaThings/registerdevice');
connection.onopen = function () {
console.log("Socket has been opened state = " + connection.readyState);
connection.send('Ping'); // Send the message 'Ping' to the server
connection.send('Websocket client');
};
console.log("Socket has been opened state = " + connection.readyState);
connection.send('finish');
// Log errors
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
// Log messages from the server
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
Java端点:
@ServerEndpoint("/registerdevice")
public class RegisterDeviceEndPoint
{
private static final Logger LOG = Logger.getLogger(RegisterDeviceEndPoint.class.getName());
@OnOpen
public void connectionOpened()
{
LOG.log(Level.INFO, "******************connection opened**************");
}
@OnMessage
public synchronized void processMessage(Session session, String message)
{
LOG.log(Level.INFO, "received message: {0}", message);
}
@OnClose
public void connectionClosed()
{
LOG.log(Level.INFO, "connection closed");
}
}
在firefox控制台上我得到了以下输出:
"Socket has been opened state = 1"
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
"Socket has been opened state = 0"
在GlassFish服务器日志上的我得到了“ping”和“Websocket客户端”,但是在onopen事件退出(不确定)之后连接关闭,因此,最后一个单词“finish”没有出现在日志和错误上发生。
我想知道我的代码是否正确? 是什么导致错误? javascript代码,GlassFish服务器配置还是java端点代码?
答案 0 :(得分:-1)
尝试更改glassfish 8080端口,例如:8887,或确保您的防病毒/其他应用程序未使用端口80,我以前曾经历过使用端口80的防病毒软件阻止我的服务器websocket。