gpt4 book ai didi

java - HTML5 Websocket 错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:47 24 4
gpt4 key购买 nike

我正在尝试使用 Netbeans 和 Apache 服务器实现 HTML5 Web 套接字..

该实现在我的笔记本电脑中工作正常,但是当我尝试使用 IP 地址访问 LAN 连接的其他计算机上的同一程序时...我在其他计算机中收到错误“连接已关闭”。

这是我的index.html 文件:

<!DOCTYPE html>

<html>
<head>
<title>Echo Chamber</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<center>
<div>
<input type="text" id="messageinput"/>
</div>
<br>
<div>
<button type="button" onclick="openSocket();" >Open</button>
<button type="button" onclick="send();" >Send</button>
<button type="button" onclick="closeSocket();" >Close</button>
</div>
<br>
<hr>
<br>
<!-- Server responses get written here -->
<div id="messages"></div>
</center>
<!-- Script to utilise the WebSocket -->
<script type="text/javascript">

var webSocket;
var messages = document.getElementById("messages");


function openSocket(){
// Ensures only one connection is open at a time
if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){
writeResponse("WebSocket is already opened.");
return;
}
// Create a new instance of the websocket
webSocket = new WebSocket("ws://localhost:8080/HTML_5_Socket_Appache/echo");

/**
* Binds functions to the listeners for the websocket.
*/
webSocket.onopen = function(event){
// For reasons I can't determine, onopen gets called twice
// and the first time event.data is undefined.
// Leave a comment if you know the answer.
if(event.data === undefined)
return;

writeResponse(event.data);
};

webSocket.onmessage = function(event){
writeResponse(event.data);
};

webSocket.onclose = function(event){
writeResponse("Connection closed");
};
}

/**
* Sends the value of the text input to the server
*/
function send(){
var text = document.getElementById("messageinput").value;
webSocket.send(text);
}

function closeSocket(){
webSocket.close();
}

function writeResponse(text){
messages.innerHTML += "<br/>" + text;
}

</script>

</body>
</html>

这是我的 java 文件:

import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
/**
* @ServerEndpoint gives the relative name for the end point
* This will be accessed via ws://localhost:8080/EchoChamber/echo
* Where "localhost" is the address of the host,
* "EchoChamber" is the name of the package
* and "echo" is the address to access this class from the server
*/
@ServerEndpoint("/echo") public class EchoServer
{
/**
* @OnOpen allows us to intercept the creation of a new session.
* The session class allows us to send data to the user.
* In the method onOpen, we'll let the user know that the handshake was
* successful.
*/
@OnOpen
public void onOpen(Session session){
System.out.println(session.getId() + " has opened a connection");
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* When a user sends a message to the server, this method will intercept the message
* and allow us to react to it. For now the message is read as a String.
*/
@OnMessage
public void onMessage(String message, Session session){
System.out.println("Message from " + session.getId() + ": " + message);
try {
session.getBasicRemote().sendText(message);
} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* The user closes the connection.
*
* Note: you can't send messages to the client from this method
*/
@OnClose
public void onClose(Session session){
System.out.println("Session " +session.getId()+" has ended");
} }

请帮助我了解我哪里出错了,这导致代码只能在我的机器上运行,而无法在网络中连接其他机器。

最佳答案

这里的问题是 Origin 验证 - 您需要将服务器设置为实际服务(静态)html 页面 - 而不仅仅是部署 websocket 端点。之后,Origin header 将添加到相同的主机(通过浏览器运行时),并且一切都应该按预期工作。

您应该使用类似于 [how to construct a websocket uri relate to the page uri] 的内容,这将允许您为您的 websocket 端点使用相对 uri(与您现在拥有的硬编码值相比)。

关于java - HTML5 Websocket 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com