0
votes

This function doesn't work in my app. I can't receive a message (socket.emit) from the server to the client (socket.on). But i don't have this problem in the inverse (client to server).

I use cloud9 and the chat example from them works fine. Here is my code for the server :

var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');

var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
router.use(express.static(path.resolve(__dirname, 'client2')));


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

  socket.emit('hello', 'i changed');  // !!!

});


server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  var addr = server.address();
  console.log("Chat server listening at", addr.address + ":" + addr.port);
});

and the code for the html page :

<html>
  <head>
    <title>test</title>

 <script src="/socket.io/socket.io.js"></script>
  <script>

  var socket = io.connect();

  socket.on('hello', function (msg) {     // !!!
        document.innerHTML = msg;
   });


    </script>
  </head>
  <body>

  <p>normal page</p>

   </body>
</html>

And there is no change... edit : with console.log either. But the message seems sent with the socket :

Debugger listening on port 15454                                                                                                                                                                                                       
   info  - socket.io started                                                                                                                                                                                                           
Chat server listening at 0.0.0.0:8080                                                                                                                                                                                                  
   debug - served static content /socket.io.js                                                                                                                                                                                         
   debug - client authorized                                                                                                                                                                                                           
   info  - handshake authorized 13XpSYGuXzMpMeyFsRfO                                                                                                                                                                                   
   debug - setting request GET /socket.io/1/websocket/13XpSYGuXzMpMeyFsRfO                                                                                                                                                             
   debug - set heartbeat interval for client 13XpSYGuXzMpMeyFsRfO                                                                                                                                                                      
   debug - client authorized for                                                                                                                                                                                                       
   debug - websocket writing 1::                                                                                                                                                                                                       
   debug - websocket writing 5:::{"name":"hello","args":["i changed"]}  

Can you help me please ?

4
Have you tried changing from document.innerHTML to a console.log()? Maybe the data is being recieved but not correctly added to the page. - Ethan
Yes I tried and it doesn't work either. - Getzel Gallagher
I think on the client you might have to specify the port (80 for http) manually, otherwise it'll try to access http://<workspace-name>-<username>.c9.io:8080. Can you see if that helps? - Mutahhir
It doesn't work. I tried to change the port to 8080 in the code : server.listen( 8080, process.env.IP || "0.0.0.0", function(){ Then 8081 and 8082, to access the webpage, I had to use your url http://<workspace-name>-<username>.c9.io:8081 etc. How can I manually specify the port 80 ? Its with the url ? It's strange because its a simple function that i try to use (emit) and it worked for me before or even when i use the chat example from Cloud9. So i don't know what is wrong. - Getzel Gallagher

4 Answers

0
votes

You need to change

var socket = io.connect();

by

var socket = io.connect('http://0.0.0.0:8080');
0
votes

var socket = io.connect() <- this should specify the domain to which you are connecting.

0
votes

So i did alot in this, trying to get it to work, it sorta did but i don't work like you do, i'm a beginner and not compatible with how you code but i've figured out that the problem is the port the io is using

var io = socketio.listen(server);

or where the html is getting the connection

<script src="/socket.io/socket.io.js"></script>

to me, the html is requesting a js file in it's own dir, but it shouldn't.

When i use socket.io, i put this in the server

var client = require("socket.io").listen(8080).sockets

The main part is the listen, it listens on the port 8080 and in my html i do

var socket = io.connect("http://127.0.0.1:8080");

Which works, i'm sorry for this, i am mostly sure this isn't an answer but i've tried, i hope you get it done, even if a year and 3 months have passed.

0
votes
    $(function(){
  var socket = io();
  socket.on('hello',function(data){
    $('#lblmsg').text(data.message);
  });
});

this is something I am working with. in the 4th line of my code, data is passed and data.message is retrieved. but you directly equated msg with the innerhtml, on the html page(in the block you commented \!!!)

see if you are still working on it!