博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Routing with restify and socket.io in node
阅读量:7236 次
发布时间:2019-06-29

本文共 1512 字,大约阅读时间需要 5 分钟。

hot3.png

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.jsexports.handle = function (req, res, next) {console.log('In Handle'); //doesn't print thisio.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?

Answer 

In app.js, try

var io = require('socket.io').listen(app.server); //app.server instead of just app

注:可能按照官网上的代码 会导致restify运行出错,按照该修改socket.io监听server才可以

转载于:https://my.oschina.net/u/574928/blog/347388

你可能感兴趣的文章
数据库管理工具
查看>>
Android新版Glide的RequestManager加载管理器
查看>>
z.lua 1.6.0 发布,会学习你习惯的 cd 命令
查看>>
一张图弄懂java线程的状态和生命周期
查看>>
Log4Net 配置
查看>>
[20180614]删除bootstrap$记录无法启动2
查看>>
js 跨域问题 汇总
查看>>
模板方法模式实践
查看>>
MongoDB 3.0.6的主,从,仲裁节点搭建
查看>>
Android/Java网络加载框架Retrofit(一)初识
查看>>
我所知道的 vue-router
查看>>
SpringFramework核心技术一(IOC:自定义一个bean的本质)
查看>>
MCMC(四)Gibbs采样
查看>>
分布式计算框架Gearman原理详解
查看>>
你不可不知的安全问题与9大趋势
查看>>
题解 P1339 【[USACO09OCT]热浪Heat Wave】
查看>>
Flask從入門到入土(一)——程序的基本結構
查看>>
Windows 10不能正常打开开始菜单问题修复
查看>>
7.73 亿 email 信息泄露,你的密码可能在里边
查看>>
JFinal-layui v1.1 更新,极速开发企业应用系统
查看>>