Node.js http聊天服务器

时间:2014-09-19 10:39:16

标签: javascript android node.js sockets node.js-stream

我尝试创建聊天服务器,它将连接到Android客户端。 1客户端在服务器上发送一些数据,服务器接收它并发送给其余的客户端。所以我尝试使用'net',但那不起作用。在服务器上我正在收听1490端口和192.168.3.XX地址。现在我的客户端可以连接到服务器,但发送数据不起作用。 代码:

var net = require('http');
var sockets = [];
var tcpServer = net.createServer();

tcpServer.on('connection', function(socket){
console.log(socket.remoteAddress+' connected');
socket.setEncoding('utf8');
sockets.push(socket);
socket.on('data', function(data){
    console.log(data);
    socket.emit('data', data);
});
});
tcpServer.listen(1490, '192.168.3.XX');

1 个答案:

答案 0 :(得分:0)

我认为您的问题出在Android客户端代码上。
Check this, hope it helps



public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 
&#13;
&#13;
&#13;