[英]Titanium - connect to socket.io Server
我正在嘗試使用用於iOS / Android的socket.io和Titanium做一個簡單的聊天應用程序。
我可以從在本地計算機上運行的socket.io中獲取示例Skript。 我可以通過瀏覽器發送和接收消息。 但是我無法連接我在Titanium中構建的IOS App。 我沒有在控制台中獲得連接狀態。
有人知道我在做什么錯。 還是我忘記了什么?
我已經用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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.