[英]Can not establish a WebSocket connection from JavaScript
The application is supposed to update a table whenever a request is made. 应用程序应该在发出请求时更新表。 I have the following code to receive notifications from server.
我有以下代码来接收来自服务器的通知。 When I run the application it shows followings in alert boxes, seems it is connected but when I call 'send' method of the notification class it does not change anything.
当我运行应用程序时,它在警告框中显示以下内容,似乎已连接但是当我调用通知类的“发送”方法时它不会改变任何内容。
Alert 1) 警报1)
windowfunction connect() {
wsocket = new WebSocket("ws://localhost:8080/Notifications");
alert("got connected");
document.getElementById("foo").innerHTML = "arraypv[0]";
wsocket.onmessage = onMessage;
}
Alert 2) 警报2)
got connected
JavaScript JavaScript的
<script type="text/javascript">
var wsocket;
function connect() {
wsocket = new WebSocket("ws://localhost:8080/Notifications");
alert("got connected");
wsocket.onmessage = onMessage;
}
function onMessage(evt) {
alert(evt);
var arraypv = evt;
alert("array" + arraypv);
document.getElementById("foo").innerHTML = arraypv[0];
}
alert("window" + connect);
window.addEventListener("load", connect, false);
</script>
Code 码
@ServerEndpoint("/Notifications")
public class Notifications {
/* Queue for all open WebSocket sessions */
static Queue<Session> queue = new ConcurrentLinkedQueue();
public static void send() {
System.err.println("send");
String msg = "Here is the message";
try {
/* Send updates to all open WebSocket sessions */
for (Session session : queue) {
session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@OnOpen
public void openConnection(Session session) {
System.err.println("in open connection");
queue.add(session);
}
@OnClose
public void closedConnection(Session session) {
System.err.println("in closed connection");
queue.remove(session);
}
@OnError
public void error(Session session, Throwable t) {
System.err.println("in error");
queue.remove(session);
}
}
Maven Maven的
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0-b08</version>
</dependency>
To send a message I use following code in one of my functions 要发送消息,我在我的一个函数中使用以下代码
Notifications.send();
Console just shows 控制台只显示
SEVERE: send 严重:发送
When I trace the connection using FireBug it shows 当我使用FireBug跟踪连接时,它会显示
Firefox can't establish a connection to the server at ws://localhost:8080/Notifications.
You forgot the trailing slash : You should connect to 你忘记了斜杠 :你应该连接到
ws://localhost:8080/Notifications/
instead of 代替
ws://localhost:8080/Notifications
(Note the trailing slash which is really important). (注意尾部斜线,这非常重要)。
Also, there are some more problems with your code. 此外,您的代码还有一些问题。 WebSocket is - like almost everything in javascript asynchroneous.
WebSocket就像 - 几乎所有javascript中的异步一样。 At the point of time where you do your
在你做你的时间点
alert("got connected");
the websocket is not actaully connected. websocket不是动态连接的。 Please attach an eventhandler to it like this
请像这样附上一个事件处理程序
wsocket.onopen = function() {
alert("got connected");
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.