Websocket连接自动关闭? [英] Websocket connection closed automatically?
问题描述
我有以下JavaScript客户端代码:
var connection = new WebSocket('ws:// localhost:8080 / OmegaThings / registerdevice');
connection.onopen = function(){
console.log(Socket has open state =+ connection.readyState);
connection.send('Ping'); //将消息'Ping'发送到服务器
connection.send('Websocket client');
};
console.log(Socket has open state =+ connection.readyState);
connection.send('finish');
//记录错误
connection.onerror =函数(错误){
console.log('WebSocket Error'+ error);
};
//记录来自服务器的消息
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,************* *****连接打开**************);
$ b @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);
}
}
我得到了以下输出:
套接字已打开状态= 1
InvalidStateError:尝试使用非对象,或者不再是可用的
Socket已经打开状态= 0
GlassFish服务器日志我得到了ping和Websocket客户端,但是在onopen事件退出(不确定)后关闭连接,因此最后一个字词finish没有出现在日志中,并且发生错误。
我想知道我的代码是否正确?
导致错误的原因是什么? JavaScript代码,GlassFish服务器配置还是Java端点代码?
我通过遵循本教程,无论如何,我不知道确切的问题,但它的工作..!
I'm newbie to the web-socket programming...
I have the following JavaScript client code:
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 endpoint:
@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");
}
}
on the firefox console I got the following output:
"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"
on the GlassFish server log I got "ping" and "Websocket client", but the connection closed after onopen event exit(not sure), thus, the last word "finish" doesn't appear on the log and the error occurs.
I want to know if my code is correct?
What causes the error? javascript code, GlassFish server configuration or the java endpoint code?
I solved this problem by by following this tutorial, anyway, I don't know the exact problem, but it works..!
这篇关于Websocket连接自动关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!