0

Random Number Stream


I need to open a stream between client and server and send 1000 random numbers (between 1 and 1000) generated on the client side (Javascript) to the server (Node.js).
  • Each time the server receives a new value, it should be output into the log.
  • The values need to be sent as utf-8 encoded strings

How is this done?

1 Answers1

0

How about socket.io? It's easy to use and you can send the data in both ways if needed.

Sever side:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
    socket.on('random_number', function (data) {
        console.log(data);
    });
});

Client side:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.emit('random_number', { number: 482 });
</script>

Just wrap the socket.emit in a loop with together with a number generator and you're ready to go ;)

wesolyromek
  • 650
  • 1
  • 6
  • 17
  • Could you clarify something for me? I've never used socket.io. How the heck does `` work on the client-side, for users who don't have socket.io installed? I looked for a copy of socket.io.js, and can't find one hosted anywhere. I looked it up, and apparently there's more to it, but I'm a little confused on it. –  Mar 15 '14 at 23:07
  • It is attaching a js file, with all the required functions to your html file. The browser downloads it and parses, adding objects like io (you can see that it hasn't been defined in your file - it's in socket.io.js) to javascript context. It's just a library the client needs to communicate with your server. – wesolyromek Mar 15 '14 at 23:14
  • Ok, now I'm really confused.. I open my web page with that script included, and the web page's content gets replaced with "Welcome to socket.io" .. Am I missing something? –  Mar 15 '14 at 23:17
  • Oh well, this is because socket.io is set up to listen at port 80 at the moment. Please check out these links: http://socket.io/#how-to-use (the first section - Using with Node HTTP server; you can copy the app script from here) and http://stackoverflow.com/questions/8689877/cant-find-socket-io-js – wesolyromek Mar 15 '14 at 23:24
  • I'm such a newbie... Trying to make Socket.io work is the first thing is javascript that makes me want to quit.. haha. This is bad. The client script you show there just replaces my page with 'Welcome to socket.io" and after reading those pages you just showed me, I still have no clue how to make this script work. I'll keep trying. –  Mar 15 '14 at 23:43
  • I just found this http://stackoverflow.com/questions/16939755/socket-io-gives-welcome-to-socket-io-message ... maybe will help –  Mar 15 '14 at 23:46
  • Try this: http://pastebin.com/TEanr3Wt. This will create a simple node server with access only to index.html which should be located in the same folder as node script. It (index) should contain the simple html and the client script I have pasted you. Hope this helps :) – wesolyromek Mar 15 '14 at 23:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49827/discussion-between-wesolyromek-and-jt0dd) – wesolyromek Mar 15 '14 at 23:49