使用socket.io桥接池php rest api
例如,我的php点公开了这些端点使用socket.io桥接池php rest api,php,angularjs,rest,socket.io,connection-pooling,Php,Angularjs,Rest,Socket.io,Connection Pooling,例如,我的php点公开了这些端点 [GET] /api/news --> [return] [ {id:1,title:'news1'},{id:1,title:'news1'} ]; [POST] /api/news?title=<string> --> [return] {id:NEW_ID,title:$title}; 这将很好地工作,除了我必须集中每段时间来获取任何新消息,所以我认为类似socket.io的东西可以帮助我 我正在努力取得成功 用户连接到io io
[GET] /api/news --> [return] [ {id:1,title:'news1'},{id:1,title:'news1'} ];
[POST] /api/news?title=<string> --> [return] {id:NEW_ID,title:$title};
这将很好地工作,除了我必须集中每段时间来获取任何新消息,所以我认为类似socket.io的东西可以帮助我
我正在努力取得成功
var socket = io('http://localhost:3700');
socket.on('news', function (data) {
$timeout(()=>{$scope.news = data;});
});
socket.on('news_posted', function (data) {
$timeout(()=>{$scope.news.push(data);});
});
$scope.post = function(title){
socket.emit('post_news', { title: title });
}
现在我不知道如何制作这样简单的服务器
var express = require("express");
var app = express();
var port = 3700;
var io = require('socket.io').listen(app.listen(port));
//setting CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8100");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key,auth-client,auth-uid,auth-token');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
io.sockets.on('connection', function (socket) {
console.log('a user connected');
///BLOCK 1
///GET ALL POSTS FROM API AND EMIT news event BACK TO USER
///
socket.on('new_post', function(msg){
///BLOCK 2
///POST title TO API AND EMIT BACK news_posted WITH THE RESPONSE
});
});
难道redis的酒吧/酒吧套装不能更好地完成这项工作吗?@Federico这是什么?
var express = require("express");
var app = express();
var port = 3700;
var io = require('socket.io').listen(app.listen(port));
//setting CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8100");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key,auth-client,auth-uid,auth-token');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
io.sockets.on('connection', function (socket) {
console.log('a user connected');
///BLOCK 1
///GET ALL POSTS FROM API AND EMIT news event BACK TO USER
///
socket.on('new_post', function(msg){
///BLOCK 2
///POST title TO API AND EMIT BACK news_posted WITH THE RESPONSE
});
});