【发布时间】:2014-02-07 11:07:20
【问题描述】:
我从我的 localhost 机器创建了一个 websocket 服务器,可用于与 android 应用程序通信。
服务器用 Node.js 编写并托管在我的机器上
var HOST = "192.168.0.15";
var PORT = 6969;
var Sock = net.Socket();
net.createServer(function(sock) {
Sock = sock;
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
var jsonStr = JSON.stringify(data);
sock.write(jsonStr);
var buffer = "";
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
io.sockets.on('connection', function(socket){
socket.on('send message', function(data){
Sock.emit("data", data);
});
});
我的 Android 应用使用 Java Websocket 和
socket = new Socket("192.168.0.15", 6969);
commsThread = new CommsThread(socket);
commsThread.start();
我用于测试我的应用的 Android 设备和我的服务器在同一个网络中运行,因此它们可以正常工作。
但是,当我在 Heroku 中部署它时,它给了我一个错误。
注意:我尝试更改主机地址和端口地址,但无济于事。
听说 Heroku 不支持 TCP 套接字服务器。
我不确定上面的例子是不是一个 TCP 套接字服务器。
如果不是,为什么我可以使用一些替代方案。
干杯, 丹尼斯
【问题讨论】:
标签: android node.js heroku websocket socket.io