Support
Quality
Security
License
Reuse
kandi has reviewed socket.io-client-java and discovered the below as its top functions. This is intended to give you an instant insight into socket.io-client-java implemented functionality, and help decide if they suit your requirements.
Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
How to connect Java SocketIO to Javascript Server
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const io = require('socket.io')(http, {
cors: { origin: '*' }
})
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'))
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();
}
}
-----------------------
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const io = require('socket.io')(http, {
cors: { origin: '*' }
})
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'))
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();
}
}
-----------------------
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const io = require('socket.io')(http, {
cors: { origin: '*' }
})
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'))
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();
}
}
-----------------------
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const io = require('socket.io')(http, {
cors: { origin: '*' }
})
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'))
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();
}
}
QUESTION
How to connect Java SocketIO to Javascript Server
Asked 2020-Dec-11 at 13:35I have a javascript server 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
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. 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
ANSWER
Answered 2020-Dec-11 at 13:35I've found my answer after the Java SocketIO client was updated. I made some mistakes.
socket.io
to be version 2.*1.*
for the Java Client, the SocketIO server must be of version 2.*
. At this time, 2.3.0
is the latest npm package for socket.io
so install that instead of version 3.*
.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. This is why when creating the const io
, we should change it to:
const io = require('socket.io')(http, {
cors: { origin: '*' }
})
This allows code of any host - or origin - to access the SocketIO server.
Here's an example of my working Java Client and Javascript Server if anyone needs the reference
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
:
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();
}
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit