首页 > 解决方案 > 以与 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://www.swoole.co.uk

https://github.com/swoole/swoole-src

https://reactphp.org

https://github.com/reactphp/socket

任何其他框架?

但不知道是不是一个好的稳定的替代方案?

谢谢!

标签: javascriptphpnode.jswebsocketsocket.io

解决方案


我知道有一个名为Ratchet的图书馆。但根据我的经验,它的效果不是很好。如果你试图建立一个套接字连接,你应该总是只使用支持它的技术,比如 NodeJs

可以使用 curl 请求从 PHP 调用 NodeJS 服务器(快速),并使 NodeJS 服务器进行套接字连接。


推荐阅读