| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

使用HTTP实现了一个类似websocket的协议:httpsocket

类似于websocket API的一个实现,采用HTTP的方式。可用于浏览器和Node环境。
var http = require('http')  var through = require('through2')  var url = require('url')  var Router = require("routes-router")  var HTTPSocket = require('./')  var HTTPSocketBackend = require('./backend')  var debug = require('debug')('HTTPSocketTestServer')  var backends = {}  var router = Router()  var server = http.createServer(router)  router.addRoute("/:id/push", function (req, res, opts) {  debug(req.method, req.url)  var backend = backends[opts.params.id]  if (!backend) backend = backends[opts.params.id] = HTTPSocketBackend()  backend.post(req, res)  })  router.addRoute("/:id/events", function (req, res, opts) {  debug(req.method, req.url)  var backend = backends[opts.params.id]  if (!backend) backend = backends[opts.params.id] = HTTPSocketBackend()  backend.events.pipe(res, {end: false})  req.on('close', function() {  backend.events.destroy()  res.end()  })  })  router.addRoute("/:id/get/:object", function (req, res, opts) {  debug(req.method, req.url)  var backend = backends[opts.params.id]  if (!backend) return res.end('no backend for that id')  backend.get(opts.params.object, res)  })  server.listen(8080, function listening() {  var client = new HTTPSocket('http://localhost:8080')  console.log('socket send', ["hello"])  client.write(new Buffer('hello'))  console.log('socket send', ["world"])  client.write(new Buffer('world'))  var pending = 2  client.pipe(through(function(chunk, enc, next) {  console.log('socket receive', [chunk.toString()])  next()  if (!--pending) {  client.end()  server.close()  }  }))  })

项目主页:http://www.open-open.com/lib/view/home/1416982642987

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1416982642987.html
WebSocket 开发 httpsocket