简体   繁体   中英

Sending data from flash to node.js server with socket.io

I have a simple socket.io based server in node.js that listens to a socket in port 81 (for example) I have no problem connecting and passing data between clients written in Javascript But after a lot of tries I can't get my Flash app to connect to the server and pass data to it (I found some examples in the web - but all of them are flex based and use classes that aren't parts of the basic AS3 libraries)

when I'm trying to connect to the socket using a XMLSocket - I get Security Sandbox Violation Error - I know that I need to load the policy file (which suppose to be available using socket.io) but I can't manage to do it

this is my flash code:

Security.loadPolicyFile("http://127.0.0.1:81/crossdomain.xml");
socket = new XMLSocket("127.0.0.1", 81);
socket.addEventListener(Event.CONNECT, onConnect);  
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);  
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityEvent);
socket.addEventListener(DataEvent.DATA, onData);

and the server side code is

var app = require('http').createServer(function (req, res) {...})
   , io = require('socket.io').listen(app, { log: true })
   , fs = require('fs')

app.listen(81);

io.sockets.on('connection', function (socket) {
    console.log("connection "+socket.id);
    socket.on("message",function(data){
        console.log("data: "+data);
    });
    socket.on("data",function(d){
         console.log('data from flash: ',d);
    });
    socket.on('disconnect', function (socket) {
        console.log("disconnect");
    });
});

and my crossdomain.xml is:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
    <site-control permitted-cross-domain-policies="master-only"/>
    <allow-access-from domain="*" to-ports="*"/>
</cross-domain-policy>

Thank you very much for your time!

For our last project, we used FlashSocket.IO , worked like a charm.

We had to do minor adaptions on the server-side (ie make sure socket.io listens for the policyfile on port 843 rather than 10843) and activate the flashsocket transport, but the communication was a breeze.

io.configure(function() {
  io.set('transports', ['websocket','flashsocket']);
  io.set('flash policy port', 843);
});

NOTE: this configuration requires node.js to be run as root!

Try this solution (you need to setup the socket policy server in addition to the game server on the port 843)

BTW: when you use socket always use url like xmlsocket://host:port (for port 843 flash loads policy file automatically) rather than http://host:port witch is only for http requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM