猫言猫语

严以律己·宽以待人·自强不息·知行合一

PHP WebSocket Server Demo

| PHP WebSocket Server Demo已关闭评论

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, "0.0.0.0", 9501);
socket_listen($socket, 5);
for(;;) {
    $client = socket_accept($socket);
    if (!pcntl_fork()) {
        $buffer = socket_read($client, 2048);
        $headers = explode("\r\n", $buffer);
        foreach($headers as $header) {
            if (strstr($header, "Sec-WebSocket-Key:")) {
                $key = trim(str_replace("Sec-WebSocket-Key:", "", $header));
                $accept = base64_encode(sha1($key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
            }
        }
 
        socket_write($client, "HTTP/1.1 101 Switching Protocols\r\n");
        socket_write($client, "Upgrade: websocket\r\n");
        socket_write($client, "Connection: Upgrade\r\n");
        socket_write($client, "Sec-WebSocket-Accept: $accept\r\n");
        socket_write($client, "\r\n");
 
        for ($i = 0;; $i++) {
            $buffer = socket_read($client, 2048);
            echo $buffer;
        }
        socket_close($client);
        exit(0);
    } else {
        socket_close($client);
    }
}
socket_close($socket);

Ubuntu 下禁用 PHP xdebug 扩展

| Ubuntu 下禁用 PHP xdebug 扩展已关闭评论

vim /etc/php/7.1/mods-available/xdebug.ini

zend_extension=xdebug.so注释掉

;zend_extension=xdebug.so

解决 iOS 系统日期/时间输入框文字无法右对齐

| 解决 iOS 系统日期/时间输入框文字无法右对齐已关闭评论

由于 iOS 系统 safari 浏览器中,类型为日期/时间的 input 默认 css 样式为 display:-webkit-inline-flex,使用 text-align: right 时无法让 input 内的文字右对齐。

为了让其支持文字右对齐需要修改 css 样式 display: block,然后就支持 text-align: right 了。

input[type=date], input[type=time] {
  display: block;
  text-align: right;
}

2 块 Arduino 开发板间 I2C 通信测试

| 2 块 Arduino 开发板间 I2C 通信测试已关闭评论

我们使用 Arduino Uno R3 作为 Master,Arduino Nano R3 作为 Slave,只要 2 根线:

Uno 的 A4 和 Nano 的 A4 连接
Uno 的 A5 和 Nano 的 A5 连接

(更多…)

Arduino 与 ENC28J60 端口连接

| Arduino 与 ENC28J60 端口连接已关闭评论

Arduino UNO

VCC -   3.3V
GND -    GND
SCK - Pin 13
SO  - Pin 12
SI  - Pin 11
CS  - Pin 10

Arduino MEGA

VCC -   3.3V
GND -    GND
SCK - Pin 52
SO  - Pin 50
SI  - Pin 51
CS  - Pin 53

PHP Web Server Demo

| PHP Web Server Demo已关闭评论

纯 Demo Web Server,一切 Web 请求均返回 HelloWorld 作为响应

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, "0.0.0.0", 9501);
socket_listen($socket, 5);
for(;;) {
    $client = socket_accept($socket);
    $buffer = socket_read($client, 2048);
 
    socket_write($client, "HTTP/1.1 200 OK\r\n");
    socket_write($client, "Transfer-Encoding: chunked\r\n");
    socket_write($client, "\r\n");
    socket_write($client, "HelloWorld\r\n");
    socket_close($client);
}
socket_close($socket);

PHP Socket Server Demo

| PHP Socket Server Demo已关闭评论

代码功能:在本地开一个 TCP 9501 端口,等待客户端连接,客户端连接上来之后,向客户端发送一个 HelloWorld,然后关闭这个连接,等待下一个客户端的连接。

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, "0.0.0.0", 9501);
socket_listen($socket, 5);
for(;;) {
    $client = socket_accept($socket);
    socket_write($client, "HelloWorld\n");
    socket_close($client);
}
socket_close($socket);

JavaScript WebSocket Client Demo

| JavaScript WebSocket Client Demo已关闭评论

纯 Demo 代码,和当年 XMLHttpRequest 似的,裸调 WebSocket:

var socket = new WebSocket('ws://localhost:9501');
socket.onopen = function(event) {
    socket.send('HelloWorld!');
    socket.onmessage = function(event) {
        console.log('Client received a message', event);
    };
    socket.onclose = function(event) {
        console.log('Client notified socket has closed', event);
    };
    //socket.close();
};