简体   繁体   English

如何将 Java SocketIO 连接到 Javascript 服务器

[英]How to connect Java SocketIO to Javascript Server

I have a javascript server server.js我有一个 javascript 服务器server.js

const http = require("http").createServer()
const io = require("socket.io")(http)   

io.on("connection", socket => {
    console.log("a user connected")
})

http.listen(3000, () => {
    console.log("listening on *:3000")
})

and a java client SocketIO.java和一个 java 客户端SocketIO.java

package chess_client;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class SocketIO {
    private static Socket socket;

    public static void main(String[] args) throws URISyntaxException {
        socket = IO.socket("http://localhost:3000");

        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                System.out.println("Connected!");
            }

        });

        socket.connect();
    }
}

I've been searching for Javascript Server & Java Client examples but the example on https://github.com/socketio/socket.io-client-java only shows the Java Client example without the Javascript Server code. I've been searching for Javascript Server & Java Client examples but the example on https://github.com/socketio/socket.io-client-java only shows the Java Client example without the Javascript Server code. When combining both, nothing works.当两者结合时,没有任何效果。 I don't see anything in either of the terminals when I run both codes???当我运行这两个代码时,我在任何一个终端中都看不到任何东西??? What's wrong???怎么了???
For any answers given, do note that I would prefer using Typescript than Javascript but both are acceptable对于给出的任何答案,请注意我更喜欢使用 Typescript 而不是 Javascript 但两者都是可以接受的

I've found my answer after the Java SocketIO client was updated.在更新 Java SocketIO 客户端后,我找到了答案。 I made some mistakes.我犯了一些错误。

  1. I need the npm package for socket.io to be version 2.*我需要 npm package 的socket.io是版本 2.*
    The documentation stated that for a version of 1.* for the Java Client, the SocketIO server must be of version 2.* .文档指出,对于 Java 客户端的1.*版本,SocketIO 服务器必须是2.*版本。 At this time, 2.3.0 is the latest npm package for socket.io so install that instead of version 3.* .此时, 2.3.0是最新的 npm package for socket.io所以安装它而不是版本3.*
  2. I'm assuming the Java Client is of a different host because it's not rendered by the server itself.我假设 Java 客户端是不同的主机,因为它不是由服务器本身呈现的。 For example, this is the code on the official SocketIO documentation:例如,这是 SocketIO 官方文档上的代码:
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

In this example, the Javascript server is directly rendering the HTML file so However, the Java code can't be run by a Javascript server.在此示例中,Javascript 服务器直接渲染 HTML 文件,因此 Java 代码不能由 Z49A2713B6ACAAAD71D2 服务器运行This is why when creating the const io , we should change it to:这就是为什么在创建const io时,我们应该将其更改为:

const io =  require('socket.io')(http, {
    cors: { origin: '*' }
})

This allows code of any host - or origin - to access the SocketIO server.这允许任何主机(或来源)的代码访问 SocketIO 服务器。


Here's an example of my working Java Client and Javascript Server if anyone needs the reference这是我的工作 Java 客户端和 Javascript 服务器的示例,如果有人需要参考
app.js : app.js

const http = require('http').createServer()

const io =  require('socket.io')(http, {
    cors: { origin: '*' }
})

io.on('connection', (socket) => {
    console.log('Connected to client!')

    io.emit('time', Date.now())
})

http.listen(8080, () => console.log('Server running on port 8080'))

App.java : App.java :

package socket_test;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class App {
    public static void main(String[] args) throws URISyntaxException {
        Socket socket = IO.socket("http://localhost:8080");
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                System.out.println("Connected to server!");
            }

        });
        socket.on("time", new Emitter.Listener() {
            
            @Override
            public void call(Object... args) {
                System.out.println("Time: " + args[0]);
            }
            
        });
        socket.connect();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM