0
我正在嘗試使用socket.io和Titanium for IOS/Android進行簡單的聊天Applikation。鈦 - 連接到socket.io服務器
我能從我的本地機器上運行的socket.io中獲得示例Skript。 我可以通過我的瀏覽器發送和接收消息。 但我無法連接我的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.