使用NODEJS连接到AS3套接字服务器

时间:2016-02-21 18:06:59

标签: node.js actionscript-3

我正在试图连接到客户端正在使用new Socket() AS3类进行连接的套接字服务器。我想在nodeJS中做类似AS3套接字类的操作,我该怎么做? 有没有现成的图书馆?

1 个答案:

答案 0 :(得分:0)

这是Node.js套接字服务器的简化版本,与我的一个项目中的flash进行通信

var net = require('net');

var json = "";

var server = net.createServer(function(socket) {
        socket.on('data', function(data) {
             try {
                    json = JSON.parse(data);
                } catch (e) {
                    json += data;

                    try {
                        json = JSON.parse(json);
                        // Now it's fine.
                    } catch (e) {
                        // Wait for more.
                        return;
                    }
                }
            //here do something with received JSON
            //then clear it for next message
            json = '';

        });
    });
});


server.listen(8888);

在这个例子中,套接字是发送文本JSON但你可以使用你想要的任何二进制格式,只需编写解析器。希望能帮助到你。 对不起,没看到。但你试图使用谷歌?有很多例子, 只需复制其中一个

var net = require('net');

var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
    console.log('Connected');
    client.write('Hello, server! Love, Client.');
});

client.on('data', function(data) {
    console.log('Received: ' + data);
    client.destroy(); // kill client after server's response
});

client.on('close', function() {
    console.log('Connection closed');
});
相关问题