定制 danielealbano/php-websocket-s2c 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

danielealbano/php-websocket-s2c

Composer 安装命令:

composer require danielealbano/php-websocket-s2c

包简介

README 文档

README

Introduction

php-websocket-s2c is a PHP Websocket native implementation able to serve requests through apache, nginx or any other webserver and proxy without any particular configuration.

The websocket support in PHP is almost absent because, usually, you need support for them at webserver/proxy level, the approach used in this implementation allows to send packets only from the server to the client and not the reverse without having to change any configuration parameter on your proxy and/or on your webserver, the s2c suffixs stands for Server To Client.

Supported features

The library supports the following features:

  • Server to Client messages (text (json), binary, ping/pong)
  • Fragmented messages (used to stream data)
  • Packet size up to 2^64
  • Try to automatically disable http compression (gzip) and chunking
  • Websocket Protocols (Sec-Websocket-Protocol header)
  • Protocol version 13 (https://en.wikipedia.org/wiki/WebSocket#Browser_implementation)

What is unsupported and/or untested:

  • Extensions (multiplexing, compression, etc.)
  • SSL/TLS

This library has been tested with PHP 5.4, PHP 5.5 and PHP 5.6 on Apache 2.2 with mod_php

Bugs

This library is very young and uses tricks to avoid starting a standalone webserver, if something doesn't work, before opening an issue, please check if your webserver is using/requesting:

  • the compression enabled (Content-Encoding header should be absent or its value should be identity);
  • the chunking enabled (Transfer-Encoding header should be absent or its value should be identity).

When you report an issue please remember to mention:

  • if Content-Length header has a value different than zero;
  • if Connection header is absent or has a different value than Upgrade;
  • if Upgrade header is absent or has a different value than websocket;
  • Your webserver (name and version);
  • If you are using php as module or as fastcgi;
  • If you are using a proxy and, if yes, which one

Examples

Plain Websocket

New the websocket server
require_once('src/WebSocket.php');
require_once('src/Frame.php');

$ws = new WebsocketS2C\WebSocket();

Using composer will not be necessary to include the sources.

Check if a websocket is requested
if ($ws->isWebsocket() == false) {
	echo 'Not a websocket!';
	die();
}
Accept connections
$ws->accept();
Send text frames (first example)
$ws->text(utf8_encode('This is a test!'));
Send text frames (second example)
for($i = 0; $i < 3; $i++) {
	$ws->text(utf8_encode('Loop ' . $i));
	sleep(1);
}
Send json frames
$ws->json([
	'hello' => 'world'
]);
Send binary (blob) frames
$ws->binary('This is a test!');
Stream data
$isFirst = true;
$ws->text('[', false, false);
foreach([ 'this', 'is', 'a', 'multipart', 'message' ] as $part) {
	if ($isFirst == false) {
		$ws->text(',', false, false);
	}

	$ws->json([
		'alot' => true,
		'of' => true,
		'data' => $part
	], false);

	$isFirst = false;
}
$ws->text(']');

统计信息

  • 总下载量: 61
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2016-08-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固