Sending data to server node.js / socket.io from JAVA

I am trying to send a message to my node.js / socket.io server via Java

Here is the code I am using to send the message

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

class socketMessage {

    public socketMessage() {
    }

    public static void send(String string) throws Exception {
        System.out.println("Sending message to Socket Server!");

        URL url = new URL("http://<mysite>:8000");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        try (OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream())) {
            System.out.println("Data Sent!");
            writer.write(string);
            writer.flush();
        }
        conn.getResponseCode(); // Thanks to BGR
    }
}

      

callable with: socketMessage.send("Admin|notification|Logged in elsewhere\n> Official Java App");

and here is the server code:

var http = require("http");
var io = require('socket.io');

var sessionsConnections = {};
var whereIsUserVisitingFrom = {};

console.log("\n\n-------------------------------- Server Started --------------------------------");

var server = http.createServer(function(req, res){ 
    var body = '';
    req.on('data', function(data){
        console.log('Received Data');
        body += data;
    });
    req.on('end', function() {
        // Emit the data to all clients
        ioServer.emit('message', { message: body });
    });
});
server.listen(8000);

var ioServer = io.listen(server, { log: false });

ioServer.on('connection', function(socket) {

    var currentUser;

    var parts;

    socket.on('started', function(data) {
        currentUser = data.username;
        sessionsConnections[currentUser] = socket.id;
        whereIsUserVisitingFrom[currentUser] = data.from;
        socket.emit("connected", { message: "Welcome, " + currentUser + "!" });

        socket.broadcast.emit("updateUsers", { message: sessionsConnections, from: whereIsUserVisitingFrom });
        socket.emit("updateUsers", { message: sessionsConnections, from: whereIsUserVisitingFrom });
    });

    socket.on('message', function(data) {
        var parts = data.message.split("|");

        socket.broadcast.emit("updateUsers", { message: sessionsConnections, from: whereIsUserVisitingFrom });
        socket.emit("updateUsers", { message: sessionsConnections, from: whereIsUserVisitingFrom });

        console.log("Message Recieved!");

        if( parts[0] == "*" ) {
            socket.broadcast.emit("display", { message: data.message });
        }else{
            if( parts[0] in sessionsConnections ) {
                io.sockets.socket(sessionsConnections[parts[0]]).emit("display", { message: data.message });
            }else{
                socket.emit("display", { message: "That user is not online!" });
            }
        }
    });

    socket.on('disconnect', function() {
        delete sessionsConnections[currentUser];
        socket.broadcast.emit("updateUsers", { message: sessionsConnections, from: whereIsUserVisitingFrom });
        socket.emit("updateUsers", { message: sessionsConnections, from: whereIsUserVisitingFrom });
    });

});

      

basically what my server is doing at the moment when I'm on my website and then send a command from my admin center in the form "{user} | {action (ie notification)} | {text / extra_param (s )} "it will send a message to the client you specified as {user} and then I have client side code that breaks this message apart and changes the UI accordingly, but I also want to do this from my Java application, and he does not work: (

any ideas

Vini

Thank you in advance

+3


source to share


2 answers


To complete an HTTP connection, the HTTP request must be read.



Insert conn.getResponseCode()

after blocktry

+1


source


After adding

conn.getResponseCode()

      



still doesn't work. Finally, I found socket.io-java-client exactly what we needed. Since socket.io doesn't use the original websocket you are using but add protocl on it and your "data" code will never fire.

0


source







All Articles