0

I have a node app which routes the request based on the url using restfy.

Now in want to bring socket into picture. But app doesnot redirect to the function when when socket.io is used. I am using FlexSocket.IO library in flex.

Here's my code snippet.

// In app.js 
var restify = require('restify')
, http = require('http')
,socket = require('./routes/socket');

var app = restify.createServer();

app.get('/', socket.handle);
app.post('/', socket.handle);

var io = require('socket.io').listen(app);

app.listen(8080, function() {
  console.log('%s listening at %s', app.name, app.url);
});
io.configure(function() {
  io.set('transports', ['websocket','flashsocket']);
  io.set('flash policy port', 843);
});
exports.io = io;

//In socket.js
exports.handle = function (req, res, next) {
console.log('In Handle'); //doesn't print this
io.sockets.on('connection', function(client){
console.log('Connection establiished');
});

In Flex

private var socket:FlashSocket;
socket = new FlashSocket("localhost:8080");
socket.addEventListener(FlashSocketEvent.CONNECT, onConnect); 
socket.addEventListener(FlashSocketEvent.MESSAGE, onMessage);

protected function onConnect(event:FlashSocketEvent):void 
{
  Alert.show('connect'); //This alert is shown.
}

Is there anything wrong with the code? Why is the socket.handle function not called in node?

Sarita
  • 837
  • 11
  • 19
  • try to configure, only "flashsocket" on server – Gntem Nov 07 '13 at 12:53
  • Yes tried that but it still doesn't work. It works only when I place io.sockets.on('connection', function (client) { var flexClient = client; .... part in app.js file. The app.get and app.post doesn't seem to work. – Sarita Nov 08 '13 at 05:47
  • try to not export variables, instead keep'em inside the app.js, and don't run it as middlewares , let client connect to server port, so it can switch to websocket server. Use namespaces `.of('/')` and others to send to a client that its on `/` route. – Gntem Nov 08 '13 at 07:07
  • @GeoPhoenix thanx for the response. I want to use the socket connection only when the particular url is called. I tried using `io.of("/socket").on("connection", function (socket) { ---- });` on node. and `socket = new FlashSocket("localhost:8080/socket");` on the flex side but then the flex app is not able to coonect to the socket and gives discovery error. I guess something wrong with the way i am connecting using FlexSocket. Can anybody help? – Sarita Nov 11 '13 at 11:18

1 Answers1

3

In app.js, try

var io = require('socket.io').listen(app.server); //app.server instead of just app
amolbk
  • 797
  • 1
  • 11
  • 23