search
HomePHP FrameworkWorkermanHow to configure wss in workerman

How to configure wss in workerman

Dec 02, 2019 am 10:11 AM
workerman

How to configure wss in workerman

How does Workerman create a wss service so that the client can use the wss protocol to connect to the communication, such as connecting to the server in the WeChat applet.

Answer:

wss protocol is actually websocket SSL, which is to add an SSL layer to the websocket protocol, similar to https (http SSL).

So you only need to enable SSL based on the websocket protocol to support the wss protocol. (Recommended learning: workererman tutorial)

Open SSL directly with Workerman

##Preparation:

1. The Workerman version is not less than 3.3.7

2. PHP has the openssl extension installed

3. The certificate (pem/crt file and key file) has been applied for and placed on the disk Any directory

Code:

<?php
require_once __DIR__ . &#39;/Workerman/Autoloader.php&#39;;
use Workerman\Worker;

// 证书最好是申请的证书
$context = array(
    // 更多ssl选项请参考手册 http://php.net/manual/zh/context.ssl.php
    &#39;ssl&#39; => array(
        // 请使用绝对路径
        &#39;local_cert&#39;                 => &#39;磁盘路径/server.pem&#39;, // 也可以是crt文件
        &#39;local_pk&#39;                   => &#39;磁盘路径/server.key&#39;,
        &#39;verify_peer&#39;                => false,
        // &#39;allow_self_signed&#39; => true, //如果是自签名证书需要开启此选项
    )
);
// 这里设置的是websocket协议(端口任意,但是需要保证没被其它程序占用)
$worker = new Worker(&#39;websocket://0.0.0.0:443&#39;, $context);
// 设置transport开启ssl,websocket+ssl即wss
$worker->transport = &#39;ssl&#39;;
$worker->onMessage = function($con, $msg) {
    $con->send(&#39;ok&#39;);
};

Worker::runAll();

Through the above code, Workerman listens to the wss protocol, and the client can connect to Workerman through the wss protocol to achieve secure instant messaging. .

Test

Open the chrome browser, press F12 to open the debugging console, enter in the Console column (or put the following code into the html page and run it with js)

// 证书是会检查域名的,请使用域名连接
ws = new WebSocket("wss://域名");
ws.onopen = function() {
    alert("连接成功");
    ws.send(&#39;tom&#39;);
    alert("给服务端发送一个字符串:tom");
};
    ws.onmessage = function(e) {
    alert("收到服务端的消息:" + e.data);
    };

Note:

1. If it cannot be started, port 443 is usually occupied. Please change it to another port. Note that the client connects after changing it to other ports. You need to bring the port number. When the client connects, the address is similar to wss://domain.com:xxx, where xxx is the port number. If you must use port 443, please use method 2 proxy to implement wss.

2. The wss port can only be accessed through the wss protocol, and ws cannot access the wss port.

3. Certificates are generally bound to domain names, so when testing, the client please use domain names to connect, and do not use IP to connect.

4. If you are unable to access, please check the server firewall.

5. This method requires PHP version >=5.6, because the WeChat applet requires tls1.2, and versions below PHP5.6 do not support tls1.2.

The above is the detailed content of How to configure wss in workerman. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.