【发布时间】:2015-03-08 16:11:20
【问题描述】:
概述
我正在与两个实体合作:(i) IRC webapp 和 (ii) IRC bot。有一个机器人变量botVariable,我有兴趣将其传递给 IRC webapp 客户端。我尝试使用socket.io 来执行此操作但失败了。 对我来说,这个问题的有趣之处在于,我尝试的不是从 IRC webapp 服务器到 webapp 客户端,而是从 bot 服务器到 webapp 客户端。 这是第一个我用过socket.io的时间。 bot 和 irc webapp 都托管在(不同的)heroku url 上。
机器人代码(服务器端)
对于机器人,我有以下代码:
var botVariable = "bot string"; //botVariable is global.
var io = require('socket.io')(HTTPS);
io.on('connection', function(botVariable) {
//When client connects for the first time, send him the value immediately
socket.emit('new_value', botVariable);
console.log(botVariable);
});
console.log("End of bot file.");
IRC 代码(客户端)
对于 IRC 客户端代码,我有以下内容:
<head>
<script src="https://bot.herokuapp.com/socket.io/socket.io.js"></script>
</head>
<h2 id="bv" align="center"></h2> // where I eventually want to show botVariable
<script>
var socket = io.connect("http://bot.herokuapp.com/"); //this is where I host the bot
socket.on('new_value', function(botVariable) {
console.log(botVariable);
cBotVariable = botVariable; //I'm trying to make cBotVariable a global variable I can refer to on the client side
});
document.getElementById("bv").innerHTML = cBotVariable;
</script>
结果/错误
当我加载网站时,botVariable 无法显示。我在控制台中收到以下错误。
> GET http://bot.herokuapp.com/socket.io/socket.io.js
(index):78
> Uncaught ReferenceError: io is not defined
当我访问 bot.herokuapp.com/socket.io/socket.io.js 时,它会显示 Could not find path: /socket.io/socket.io.js。
我的印象是您实际上并不需要提供此文件,而是服务器以某种方式即时创建它。我猜不是这样吧?
【问题讨论】:
标签: javascript socket.io irc