【发布时间】:2014-07-06 14:54:23
【问题描述】:
我正在尝试使用 socket.io 和 Titanium 为 IOS/Android 制作一个简单的聊天应用程序。
我能够从在我的本地机器上运行的 socket.io 获取示例 Skript。 我可以通过浏览器发送和接收消息。 但我无法连接我在 Titanium 中构建的 IOS 应用程序。 我没有在控制台中获得连接状态。
有人知道我做错了什么吗?还是我忘记了什么?
我用 Titanium.Network.Socket.TCP 函数试过了:
var hostname = '127.0.0.1';
var clientSocket = Ti.Network.Socket.createTCP({
host : hostname,
port : 3000,
connected : function(e) {
Ti.API.info('Client socket connected!');
Ti.Stream.pump(e.socket, pumpCallback, 1024, true);
e.socket.write(Ti.createBuffer({
value : 'A message from a connecting socket.'
}));
},
error : function(e) {
Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
}
});
function writeCallback(e) {
Ti.API.info('Successfully wrote to socket.');
}
function pumpCallback(e) {
// Has the remote socket closed its end?
if (e.bytesProcessed < 0) {
Ti.API.info("Closing client socket.");
clientSocket.close();
return;
}
try {
if (e.buffer) {
var received = e.buffer.toString();
Ti.API.info('Received: ' + received);
} else {
Ti.API.error('Error: read callback called with no buffer!');
}
} catch (ex) {
Ti.API.error(ex);
}
}
Ti.API.info("Setting timer to connect.");
setTimeout(function(e) {
Ti.API.info("Calling connect on client socket.");
clientSocket.connect();
}, 500);
还有来自我服务器的代码:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
钛输出:
[INFO] : Setting timer to connect.
[INFO] : Calling connect on client socket.
[INFO] : Client socket connected!
[INFO] : Closing client socket.
【问题讨论】:
标签: ios sockets socket.io titanium