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

标签 java websocket wildfly-8

有没有办法让一个 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/

相关文章:

java - 设计更好的 API?

java - jar文件中的奇怪包

javascript - WebSocket 连接问题

mongodb - Wildfly 8 中用于 CDI 的自定义 jndi 对象工厂

java - 空指针异常(未解决)和无法将 WSResponse 映射到 play 2.4 中的结果(这部分已解决。我猜)

java - 简单的 Java 代码,无法创建 C++ 等效代码(私有(private)静态成员和公共(public)访问器方法)

ruby - padrino && websockets

javascript - Websocket 无法在 Chrome/Firefox 上发送/接收消息,在 Microsoft Edge 上工作正常

Wildfly 的 RabbitMQ 配置