简体   繁体   中英

Socket data from Node.js is not received in the Flash client

I'm having a problem to receive data from socket.io to Flash (actionscript).

I've tried:

Server (nodejs) socket.io :

var io = require('socket.io').listen(9000);
setInterval(function() {
io.sockets.emit('hello');
console.log("test");
}, 500 );

Client (actionscript) Socket();

import flash.net.Socket;
import flash.events.*;
var socket:Socket = new Socket();
Security.allowDomain("*");
socket.connect("localhost", 9000);
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
function onConnect( evt:Event ):void
{
trace( "Connected to flash");
}
function socketDataHandler(evt:ProgressEvent):void  
{  
trace("socketDataHandler: " + evt);
}  

Output

Flash connects succesful to the server and trace "Connected to flash" but it cannot receive data. Any ideas?

See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html#includeExamplesSummary and look at the method called readResponse .

You need to read the bytes from the stream.

Or just use that very example and you'll know that there is not something wrong with the client-side functionality.

Well I don't think that will work, because socket.io has it's own protocol which uses several transport methods such as websocket. But your action script is just having a normal flash socket I believe. The best is that you use the normal socket.io javascript client and and use a messaging system to communicate with your action script client. In this case, I think this link is useful for you, in which describes this messaging.

Update:

These libraries may be what you are looking for: https://github.com/ascorbic/socket-io-actionscript https://github.com/simb/FlashSocket.IO

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