原創(chuàng)作者:direction
1.準(zhǔn)備工作
l Centos 6.5建立的Wesocket服務(wù)器:
這里使用的是PHP7.1 +Swoole作為websocket服務(wù)器
l 破解了的微信開發(fā)工具
l 微信小程序文檔
2.Websocket搭建
2.1.Linux環(huán)境下安裝
Linux操作系統(tǒng)通常都有自己的包管理軟件(Ubuntu的apt-get,CentOS的yum,Mac OSX的HomeBrew等),因此一般情況下可以通過這些包管理軟件直接安裝PHP。但是這樣安裝的PHP不太適用于運(yùn)行Swoole,因此本章將介紹如何通過源碼編譯安裝。
編譯環(huán)境
想要編譯安裝PHP首先需要安裝對應(yīng)的編譯工具。 Ubuntu上使用如下命令安裝編譯工具和依賴包:
sudo apt-get install
build-essential
gcc
g++
autoconf
libiconv-hook-dev
libmcrypt-dev
libxml2-devel
libmysqlclient-dev
libcurl4-openssl-dev
libjpeg8-dev
libpng12-dev
libfreetype6-dev \
PHP安裝
PHP下載地址 在這里挑選你想用的版本即可。下載源碼包后,解壓至本地任意目錄(保證讀寫權(quán)限)。
使用如下命令編譯安裝PHP:
cd php-5.6.22/
./configure --prefix=/usr/local/php
--with-config-file-path=/etc/php
--enable-fpm
--enable-pcntl
--enable-mysqlnd
--enable-opcache
--enable-sockets
--enable-sysvmsg
--enable-sysvsem
--enable-sysvshm
--enable-shmop
--enable-zip
--enable-soap
--enable-xml
--enable-mbstring
--disable-rpath
--disable-debug
--disable-fileinfo
--with-mysql=mysqlnd
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
--with-pcre-regex
--with-iconv
--with-zlib
--with-mcrypt
--with-gd
--with-openssl
--with-mhash
--with-xmlrpc
--with-curl
--with-imap-ssl
sudo make
sudo make install
sudo mkdir /etc/php
sudo cp php.ini-development /etc/php/php.ini
注意,以上PHP編譯選項(xiàng)根據(jù)實(shí)際情況可調(diào)整。
另外,還需要將PHP的可執(zhí)行目錄添加到環(huán)境變量中。 使用Vim/Sublime打開~/.bashrc,在末尾添加如下內(nèi)容:
export PATH=/usr/local/php/bin:$PATHexport PATH=/usr/local/php/sbin:$PATH
保存后,終端輸入命令:
source~/.bashrc
此時(shí)即可通過php --version查看php版本。
2.2.Mac環(huán)境下安裝
Mac系統(tǒng)自帶PHP,但是Mac上對于OpenSSL的相關(guān)功能做了一些限制,使用了一個(gè)Secure Transport來取代OpenSSL。因此仍然建議重新編譯安裝PHP環(huán)境。
安裝OpenSSL
Mac原裝的0.9.8版本的OpenSSL使用的時(shí)候會(huì)有些Warning,反正我看不慣……
安裝命令:
brew install openssl
安裝之后,還需要鏈接新的openssl到環(huán)境變量中。
brew link --force openssl
安裝Curl
Mac系統(tǒng)原裝的Curl默認(rèn)使用了Secure Transport,導(dǎo)致通過option函數(shù)設(shè)置的證書全部無效。果斷重新安裝之。
brew install curl --with-openssl && brew link curl --force
安裝PHP
PHP官網(wǎng)上下載某個(gè)版本的PHP(我選擇的是5.6.22),使用如下命令編譯安裝。
cd /path/to/php/
./configure
--prefix=/usr/local/php
--with-config-file-path=/etc/php
--with-openssl=/usr/local/Cellar/openssl/1.0.2g/
--with-curl=/usr/local/Cellar/curl/7.48.0/
make && make install
這里我僅列出兩個(gè)需要特殊設(shè)置的選項(xiàng)with-openssl和with-curl。 安裝完成后,執(zhí)行如下命令:
sudo cp /usr/local/php/bin/php /usr/bin/
sudo cp /usr/local/php/bin/phar* /usr/bin/
sudo cp /usr/local/php/bin/php-config /usr/bin/
sudo cp /usr/local/php/bin/phpize /usr/bin/
隨后,設(shè)置php.ini
sudo mkdir /etc/php
sudo cp php.ini.development /etc/php/php.ini
2.3.Swoole擴(kuò)展安裝
Swoole擴(kuò)展下載地址 解壓源碼至任意目錄,執(zhí)行如下命令:
cd swoole-src-swoole-1.7.6-stable/
phpize
./configure
sudo make
sudo make install
swoole的./configure有很多額外參數(shù),可以通過./configure --help命令查看,這里均選擇默認(rèn)項(xiàng))
安裝完成后,進(jìn)入/etc/php目錄下,打開php.ini文件,在其中加上如下一句:
extension=swoole.so
隨后在終端中輸入命令php -m查看擴(kuò)展安裝情況。如果在列出的擴(kuò)展中看到了swoole,則說明安裝成功。
2.4.可能出現(xiàn)的問題
在CentOS編譯PHP5的時(shí)候有時(shí)會(huì)遇到以下的一些錯(cuò)誤信息,基本上都可以通過yum安裝相應(yīng)的庫來解決。以下是具體的一些解決辦法:
checking for BZip2 support… yes checking for BZip2 in default path… not found configure: error: Please reinstall the BZip2 distribution
Fix: yum install bzip2-devel
checking for cURL support… yes checking if we should use cURL for url streams… no checking for cURL in default path… not found configure: error: Please reinstall the libcurl distribution – easy.h should be in/include/curl/
Fix: yum install curl-devel
checking for curl_multi_strerror in -lcurl… yes checking for QDBM support… no checking for GDBM support… no checking for NDBM support… no configure: error: DBA: Could not find necessary header file(s).
Fix: yum install db4-devel
checking for fabsf… yes checking for floorf… yes configure: error: jpeglib.h not found.
Fix: yum install libjpeg-devel
checking for fabsf… yes checking for floorf… yes checking for jpeg_read_header in -ljpeg… yes configure: error: png.h not found.
Fix: yum install libpng-devel
checking for png_write_image in -lpng… yes If configure fails try –with-xpm-dir=
configure: error: freetype.h not found.
Fix: Reconfigure your PHP with the following option. --with-xpm-dir=/usr yum install freetype-devel
checking for png_write_image in -lpng… yes configure: error: libXpm.(a|so) not found.
Fix: yum install libXpm-devel
checking for bind_textdomain_codeset in -lc… yes checking for GNU MP support… yes configure: error: Unable to locate gmp.h
Fix: yum install gmp-devel
checking for utf8_mime2text signature… new checking for U8T_DECOMPOSE… configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.
Fix: yum install libc-client-devel
checking for LDAP support… yes, shared checking for LDAP Cyrus SASL support… yes configure: error: Cannot find ldap.h
Fix: yum install openldap-devel
checking for mysql_set_character_set in -lmysqlclient… yes checking for mysql_stmt_next_result in -lmysqlclient… no checking for Oracle Database OCI8 support… no checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h’ not found!
Fix:yum install unixODBC-devel
checking for PostgreSQL support for PDO… yes, shared checking for pg_config… not found configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
Fix: yum install postgresql-devel
checking for sqlite 3 support for PDO… yes, shared checking for PDO includes… (cached) /usr/local/src/php-5.3.7/ext checking for sqlite3 files in default path… not found configure: error: Please reinstall the sqlite3 distribution
Fix: yum install sqlite-devel
checking for utsname.domainname… yes checking for PSPELL support… yes configure: error: Cannot find pspell
Fix: yum install aspell-devel
checking whether to enable UCD SNMP hack… yes checking for default_store.h… no
checking for kstat_read in -lkstat… no checking for snmp_parse_oid in -lsnmp… no checking for init_snmp in -lsnmp… no configure: error: SNMP sanity check failed. Please check config.log for more information.
Fix: yum install net-snmp-devel
checking whether to enable XMLWriter support… yes, shared checking for xml2-config path… (cached) /usr/bin/xml2-config checking whether libxml build works… (cached) yes checking for XSL support… yes, shared configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
Fix: yum install libxslt-devel
configure: error: xml2-config not found. Please check your libxml2 installation.
Fix: yum install libxml2-devel
checking for PCRE headers location… configure: error: Could not find pcre.h in /usr
Fix: yum install pcre-devel
configure: error: Cannot find MySQL header files under yes. Note that the MySQL client library is not bundled anymore!
Fix: yum install mysql-devel
checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h’ not found!
Fix: yum install unixODBC-devel
checking for pg_config… not found configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
Fix:yum install postgresql-devel
configure: error: Cannot find pspell
Fix: yum install pspell-devel
configure: error: Could not find net-snmp-config binary. Please check your net-snmp installation.
Fix: yum install net-snmp-devel
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
Fix: yum install libxslt-devel
編譯安裝PDO_MYSQL拓展模塊,總是提示
這是因?yàn)檫@是因?yàn)樵诰幾g時(shí)需要 MySQL 的頭的文件。而它按默認(rèn)搜索找不到頭文件的位置,所以才出現(xiàn)這個(gè)問題。通過軟連接把MySQL頭文件對應(yīng)到/usr/local/include/下就好
比如你的MySQL安裝文件位于/usr/local/mysql,那么就執(zhí)行以下命令:
ln -s /usr/local/mysql/include/* /usr/local/include/
當(dāng)一個(gè)人找不到出路的時(shí)候,最好的辦法就是將當(dāng)前能做好的事情做到極致,做到無人能及。
3.微信小程序的開發(fā)
1.編寫websocket服務(wù)程序
// 建立websocket 服務(wù),啟用9502端口
$ws = new swoole_websocket_server("192.168.94.155", 9502);
//監(jiān)聽鏈接
$ws->on('open', function ($ws, $request) {
var_dump($request->fd, $request->get, $request->server);
$ws->push($request->fd, "hello, welcome\n");
});
//監(jiān)聽消息,這里就直接回顯
$ws->on('message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
});
// 關(guān)閉
$ws->on('close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();
2.在服務(wù)上啟動(dòng)。輸入phpwebsocket.php
3.開發(fā)微信小程序websocket客戶端
Web-socket.js 代碼:
var socketOpen=false;
var socketMsgQueue = [];
Page({
data:{
items:[
],
},
onReady: function() {
wx.connectSocket({
url:"ws://192.168.94.155:9502",
success:function(res)
{
console.log('ok');
wx.onSocketOpen(function(res){
socketOpen=true;
for(var i = 0 ; i <socketMsgQueue.length; i++){
this.sendSocketMessage( socketMsgQueue[i])
}
socketMsgQueue = [];
}),
wx.onSocketMessage(function(res){
var thisapp = getApp();
var thispage = thisapp.getCurrentPage();
var changedData ={}
console.log( thispage.data.items.length);
var index = thispage.data.items.length;
changedData['items[' + index + '].text'] = res.data;
thispage.setData(changedData)
console.log("收到服務(wù)器內(nèi)容:" + res.data)
})
},
fail: function(res){
console.log(res);
}
});
},
sendSocketMessage:function(msg)
{
if(socketOpen){
wx.sendSocketMessage({
data:msg
});
}else{
socketMsgQueue.push(msg)
}
},
bindSendMessage:function(e)
{
console.log(e);
var msg =e.detail.value.msg;
this.sendSocketMessage(msg);
}
})
Web-socket.wxml:
<import src="../common/header.wxml" />
<import src="../common/footer.wxml" />
<view class="container">
<template is="header" data="{{title: 'WebSocket'}}"/>
<form bindsubmit="bindSendMessage" bindreset="formReset">
<view class="page-body">
<view class="page-body-wording">
<text class="page-body-text" bindtap="sendSocketMessage" >
搭建好自己的后臺(tái)服務(wù)器后,可以跟服務(wù)器建立持久連接,實(shí)現(xiàn)即時(shí)聊天等功能。
</text>
</view>
<view wx:for="{{items}}">
{{index}}: {{item.text}}
</view>
<input placeholder="Your Message" focus="true" name="msg" />
<view class="btn-area">
<button formType="submit" >Send</button>
</view>
</view>
</form>
<template is="footer" />
</view>
最后結(jié)果如圖:
這里寫圖片描述