简体   繁体   中英

Real time text using node.js

I have a node.js server that connects to an IRC channel. I can successfully output all messages from the channel to the console, but I'd like the messages to be displayed in real time on a webpage.

I am looking into socket.io but can't come up with anything, is this the best way?

All I need to know is how to update text on the webpage in realtime, I can see the messages if I refresh the page, but it is 1 message at a time.

I believe I need a client script for this, but I am unsure where to start. Thanks!

// Get the lib
var irc = require("irc");

// Create the bot name
bot.addListener("message#", function(nick, to, text, message) {
    console.log(nick, " :=> ", text);
});

var http = require('http');
http.createServer(function (req, res) {

  var bot = new irc.Client(config.server, config.nick, config);

bot.addListener("message#", function(nick, to, text, message) {
    console.log(nick, " :=> ", text);
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write(text);
    res.end(text);
});
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

socket.io is supporting serverside AND clientside communication through its inbuilt lib, when you start the server, the script is in

/socket.io/socket.io.js

And can be started with

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
  socket.connect('http://localhost:8080', { autoConnect: true});
</script>

See http://socket.io/get-started/chat/ for a simple chat application, should be easily able to implement IRC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM