1

有没有办法让 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 连接到另一个地址的另一个服务器。但是,程序在尝试建立新连接时会停止。是我的思维有缺陷还是这种联系是不可能的?

4

1 回答 1

0

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

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();
        }
    }
于 2014-06-18T15:10:50.640 回答