[英]Real time text using node.js
I have a node.js server that connects to an IRC channel. 我有一个连接到IRC通道的node.js服务器。 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? 我正在研究socket.io,但什么都没想,这是最好的方法吗?
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. 我需要知道的是如何实时更新网页上的文本,如果刷新页面可以看到消息,但是一次只能看到1条消息。
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/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. 有关简单的聊天应用程序,请参见http://socket.io/get-started/chat/ ,应该可以轻松实现IRC。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.