javascript - 以与 Node.js 类似的方式在 PHP 上使用套接字
问题描述
我刚刚下载并运行了非常基本的Node.js
聊天应用程序:
https://github.com/socketio/chat-example
它工作正常。
服务器代码很简单:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
现在我的问题是是否可以做类似的事情PHP
?
我听说过:
https://github.com/swoole/swoole-src
https://github.com/reactphp/socket
任何其他框架?
但不知道是不是一个好的稳定的替代方案?
谢谢!
解决方案
我知道有一个名为Ratchet的图书馆。但根据我的经验,它的效果不是很好。如果你试图建立一个套接字连接,你应该总是只使用支持它的技术,比如 NodeJs
可以使用 curl 请求从 PHP 调用 NodeJS 服务器(快速),并使 NodeJS 服务器进行套接字连接。
推荐阅读
- java - 我希望能够按下一个按钮,然后使不同的框架可见并隐藏我正在使用的框架
- python - python中使用Maclaurin系列e^x的实现
- pointers - 解组嵌套结构和类型断言
- html - 全宽和全高页面中的菜单 + 滑块
- vue.js - 如何使用 v-for 在 vuejs 中显示前 6 个项目
- excel - Coldfusion CSV to Excel CSVtoArray 无关引号
- c# - 使用 C# Ionic.zip 库压缩 C 盘和 D 盘时排除某些文件夹
- spring-batch - 如何使用 Mockito 编写 Spring Batch 测试用例?
- node.js - 当我尝试在 Heroku 中构建 NodeJS 应用程序时,Typescript 上出现错误 TS2307
- python-3.x - 从 Python 自动化 IAR 编译、链接和执行