Y. Chainsan
a language, a world.

Record my voice

Sign In
In cases that a webpage need get realtime data from server side but not using the unfriendly polling way. WebSocket becomes the better one. Please kindly note that the solution I post here still need further consideration and evaluation, as it will create a large number instances of Swoole\Coroutine\Redis class if the number of users increase greatly. Check the following code carefully and let me know the deficiences of the solution, as I don't have enough swoole coroutine knowledges. Also if you have better idea, please feel free to let me know by adding your comments. The WsServer.php file <?php class WsServer { public $server = null; public $table = null; public function __construct() { $this->server = new Swoole\WebSocket\Server("", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP | SWOOLE_SSL); $this->server->set([ 'ssl_cert_file' => 'path to your ssl certificate', 'ssl_key_file' => 'path to your ssl certificate key', ]); $this->table = new swoole_table(1024); $this->table->column('fd', swoole_table::TYPE_INT); $this->table->create(); $this->server->on('open', [$this, 'onOpen']); $this->server->on('message', [$this, 'onMessage']); $this->server->on('request', [$this, 'onRequest']); $this->server->on('close', [$this, 'onClose']); $this->server->start(); } public function onOpen($server, $request) { echo "server: handshake success with fd{$request->fd}".PHP_EOL; } public function onMessage($server, $frame) { echo "receive from {$frame->fd}:{$frame->data}, opcode:{$frame->opcode}, fin:{$frame->finish}".PHP_EOL; $redis = new Swoole\Coroutine\Redis();
2020-05-05 0 59
一、缘起 最近在开发一个web应用项目,在处理登录和会话问题的时候打算结合之前项目中的会话管理机制,重新整理和总结一下过往项目中有关实现方案的优缺点,并设计一个较为合理的认证鉴权服务。本文将鉴权问题简化为判断客户端是否登录的问题,项目中其他需要考虑的因素,因不属于核心问题,暂不讨论。 二、业界的主流方案 主要有两大类,一类是在会话服务器中存储session信息,客户端携带cookie,通过与服务端存储的session比对,判断用户是否登录;另一类方案采用算法校验,如JWT,其特点是服务端不专门存储会话信息,而是通过与客户端传递的部分用户信息、时间戳和token,通过约定算法,比对token是否合法; 这两类方案各有特点,不文不做详细比较。 三、PHP session机制 PHP 本身提供了一个不错的会话管理机制,属于前述讨论的服务端存储session信息方案。在程序中执行session_start()后便会生成一个会话文件,保存在某个tmp/路径下(因操作系统而异),文件名称为sess_{sessid},此处sessid是一个长度为26的唯一字符串,在当前会话结束前,可以用session_set()方法设定一个key-value数据,将其保存在该会话文件内,而session_destroy()方法可以清除该会话文件。 在实际系统运行中,用户很多时候不会主动点击退出来清除服务端的会话文件,而这些文件往往并不会主动清除,随着用户大增,时间久了会累积许多过期的会话文件,造成存储资源的浪费。当然,这些失效的垃圾文件可以通过定期执行shell脚本进行清除,或者通过php的相关设置方法进行优化,但是考虑到使用习惯和易维护性。可以考虑将session信息保存到数据库中,如redis缓存。 参考资源: 关于PHP中Session文件过多的问题 PHP Session可能会引起并发问题 网站登录鉴权的实现 记住密码功能如何设计?
2020-04-18 0 68
A Wemos D1 Mini board based on ESP8266-EX MCU was used at this expriment. The dev board looks like this Target This demo just shows how an ESP8266 device can communicate with remote server. So my local PC is used as server host in dev environment. In the next phase, I'll move the server to a cloud host like Aliyun.   Client Side The IDE is arduino, full code pasted as following. #include <ESP8266WiFi.h> #ifndef STASSID #define STASSID "hugo_school" #define STAPSK "secret" #endif const char* ssid = STASSID; const char* password = STAPSK; const char* host = ""; const uint16_t port = 2019; void setup() { // put your setup code here, to run once: Serial.begin(115200); // We start by connecting to a WiFi network delay(20000); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */ WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { Serial.print("connecting to "); Serial.print(host); Serial.print(':'); Serial.println(port); // Use WiFiClient class to create TCP connections WiFiClient client; if (!client.connect(host, port)) { Serial.println("connection failed"); delay(5000); return; } // This will send a st
2019-11-22 0 275