gpt4 book ai didi

java - Websocket 服务器初始化与另一个 websocket 服务器的连接

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

有没有办法让一个 websocket 服务器连接到另一个 websocket 服务器?我用 Java 编写了这段代码,但它不起作用。我没有收到任何错误或异常,它只是永远等待连接。

@OnMessage
public void message(Session session, String msg){
String URL = "ws://wildfly2-ciri.rhcloud.com:8000/echo";
try {
System.out.println("**1 Got new message: " + msg);
String forward = "This is WildFly 1: " + msg;
System.out.println("**1 Init new session");
Session newSession = session.getContainer().connectToServer(Client.class, URI.create(URL));
System.out.println("**1 Sending to wildfly2");
newSession.getBasicRemote().sendText(forward);

} catch (Exception e) {
e.printStackTrace();
}
}

基本上,我希望该服务器初始化与另一个地址的另一个服务器的新 websocket 连接。但是,当程序尝试建立新连接时,它会停止。是我的想法有问题还是这种联系不可能?

最佳答案

您可能会发现这很有用。这是我用来在客户端和服务器之间进行通信的旧套接字程序之一。我已附上发送 XML 文件的程序的客户端和代码。但是,您需要编辑一些内容才能让她为您工作。使用文件来玩这个并感受一下套接字并将其应用到您的程序中。快乐学习我的 friend !

public static void main(String[] args) throws IOException {
Socket socket = null;
String host = "127.0.0.1";

socket = new Socket(host, 4444);

File file = new File("C:\\testXML.xml");
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large.");
}
byte[] bytes = new byte[(int) length];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

int count;

while ((count = bis.read(bytes)) > 0) {
out.write(bytes, 0, count);
}

out.flush();
out.close();
fis.close();
bis.close();
socket.close();
}
}

public class Server {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(4444);
} catch (IOException ex) {
System.out.println("Can't setup server on this port number. ");
}

Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int bufferSize = 0;

try {
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}

try {
is = socket.getInputStream();

bufferSize = socket.getReceiveBufferSize();
System.out.println("Buffer size: " + bufferSize);
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}

try {
fos = new FileOutputStream("C:\\xxxXXXXxxx.txt");
bos = new BufferedOutputStream(fos);

} catch (FileNotFoundException ex) {
System.out.println("File not found. ");
}

byte[] bytes = new byte[bufferSize];

int count;

while ((count = is.read(bytes)) > 0) {
bos.write(bytes, 0, count);
}

bos.flush();
bos.close();
is.close();
socket.close();
serverSocket.close();
}
}

关于java - Websocket 服务器初始化与另一个 websocket 服务器的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24288796/

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