【问题标题】:Using Socket.io with Heroku使用 Socket.io 和 Heroku
【发布时间】:2013-11-28 02:23:36
【问题描述】:

Javascript:

var express = require('express')
    , app = express()
    , server = require('http').createServer(app)
    , io = require('../../lib/socket.io').listen(server)
    , dgram = require('dgram');

server.listen(5000);

var client = dgram.createSocket('udp4');
var message = new Buffer("100 101 102");

app.use(express.static(__dirname + '/'));

io.sockets.on('connection', function(socket) {
    console.log("Server Connected");
    socket.on('message', function(data) {
        console.log(data);
        client.send(message, 0, message.length, 8888, '192.168.1.1')
    });
});

相关 HTML:

$(window).load(function(){
    socket = io.connect('/');
    $(window).click(function() {
        sendData(100, 101, 102);
    });
});

function sendData(redVal, greenVal, blueVal) {
    socket.emit('message', redVal + " " + greenVal + " " + blueVal);
}

package.json:

{
    "name": "LEDControl",
    "description": "First attempt at mobile node.js and socket.io arduino control",
    "version": "0.0.1-6",
    "dependencies": {
        "express": "3.x",
        "socket.io": "~0.9.16"
    },
    "engines": {
        "node": "0.10.x",
        "npm": "1.3.x"
    }
}

我正在尝试通过 socket.io 将 udp 数据包发送到带有以太网屏蔽的 Arduino。我在 Heroku 上运行它。它在 localhost 上运行良好,但是当我尝试在 Heroku 上运行它时它总是崩溃。问题似乎与socket.io有关。 Websockets 是 Heroku 的一项新功能,因此很难找到有关它的大量信息。我也在 Nodejitsu 上试过这个,得到了类似的结果。有人知道我在做什么错吗?现在,只要我点击屏幕,它就应该发送“100 101 102”。问题可能出在 Arduino Ethernet Shield 的限制上,我不确定。

【问题讨论】:

    标签: node.js heroku socket.io arduino


    【解决方案1】:

    您是否为您的应用启用了 websockets (heroku labs:enable websockets -a appname)?

    【讨论】:

    • 是的,websockets 已启用。
    猜你喜欢
    • 2012-06-19
    • 2014-11-25
    • 2023-03-23
    • 2020-02-10
    • 1970-01-01
    • 2013-03-16
    • 2016-08-02
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多