alexkratky / php-socketio
PHP 类,用于与 Socket.io 一起使用。
v1.0.0
2020-05-14 08:56 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-14 18:22:00 UTC
README
用于实时 socket(Socket.io)操作的类。
安装
composer require alexkratky/php-socketio
用法
require 'vendor/autoload.php'; use AlexKratky\PHPSocketIO; $ip = "localhost"; $port = 8000; $password = "password"; $secured = true; //https? $channel = "room1"; $data = array( "ID" => 1, "name" => "Alex" ); $socket = new PHPSocketIO($ip, $port, $password, $secured); $socket->sendData($channel, $data);
NodeJS 实现
您可以通过这里了解 NodeJS 实现。一般来说,您需要处理 HTTP POST 请求。
var fs = require('fs'); var http = require('http'); var bodyParser = require("body-parser"); var express = require('express'); var app = express(); var port = 8000; var urlencodedParser = bodyParser.urlencoded({ extended: false }); var webServer = http.createServer(app); var io = require('socket.io').listen(webServer, { log: false }); app.post('/', urlencodedParser, function (req, res) { if (!req.body) return res.sendStatus(400); var post = req.body; if (post.password == config.password) { io.emit(post.channel, JSON.parse(post.data)); res.send('true'); } else { res.sendStatus(400); } });