title: sending data to node.js/socket.io server from JAVA
body: <p>I am trying to send a message to my node.js/socket.io server via Java</p>
  
  <p>Here is the code i am using to send the message</p>
  
  <pre><code>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://&lt;mysite&gt;: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
      }
  }
  </code></pre>
  
  <p>called with: <code>socketMessage.send("Admin|notification|Logged in elsewhere\n&gt; Official Java App");</code></p>
  
  <p>and here is the server code: </p>
  
  <pre><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 });
      });
  
  });
  </code></pre>
  
  <p>basically what my server does currently is when I visit my website, then send a command from my Admin Center in the form of "{user}|{action (i.e. notification)}|{text/extra_param(s)}" it will send a message to the client of whom you set as the {user} and then I have code on the client side that splits that message into parts and changes the websites UI accordingly, but I also want to do this from my Java app, and it ain't working :(</p>
  
  <p>any ideas</p>
  
  <p>Vinny</p>
  
  <p>Thanks in advance</p>