[代码]const app = getApp();[代码][代码]const config = app.config;[代码][代码]const wafer = require([代码][代码]'../../vendors/wafer-client-sdk/index'[代码][代码]);[代码][代码]const lab = require([代码][代码]'../../lib/lab'[代码][代码]);[代码] [代码]Page({[代码][代码] [代码][代码]data: {[代码][代码] [代码][代码]status: [代码][代码]'waiting'[代码][代码],[代码][代码] [代码][代码]url: [代码][代码]'wss://'[代码] [代码]+ config.host + [代码][代码]'/ws'[代码][代码],[代码][代码] [代码][代码]connecting: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]hintLine1: [代码][代码]'完成服务器开发,'[代码][代码],[代码][代码] [代码][代码]hintLine2: [代码][代码]'让服务器支持 WebSocket 连接'[代码][代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* WebSocket 是否已经连接[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]socketOpen: [代码][代码]false[代码][代码],[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 开始连接 WebSocket[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]connect() {[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]status: [代码][代码]'waiting'[代码][代码],[代码][代码] [代码][代码]connecting: [代码][代码]true[代码][代码],[代码][代码] [代码][代码]hintLine1: [代码][代码]'正在连接'[代码][代码],[代码][代码] [代码][代码]hintLine2: [代码][代码]'...'[代码][代码] [代码][代码]});[代码][代码] [代码][代码]this[代码][代码].listen();[代码][代码] [代码][代码]wafer.setLoginUrl(`https:[代码][代码]//${config.host}/login`);[代码][代码] [代码][代码]wafer.login({[代码][代码] [代码][代码]success: () => {[代码][代码] [代码][代码]const header = wafer.buildSessionHeader();[代码][代码] [代码][代码]const query = Object.keys(h[图片] eader).map(key => `${key}=${encodeURIComponent(header[key])}`).join([代码][代码]'&'[代码][代码]);[代码][代码] [代码][代码]wx.connectSocket({[代码][代码] [代码][代码]// 小程序 wx.connectSocket() API header 参数无效,把会话信息附加在 URL 上[代码][代码] [代码][代码]url: `${[代码][代码]this[代码][代码].data.url}?${query}`,[代码][代码] [代码][代码]header[代码][代码] [代码][代码]});[代码][代码] [代码][代码]},[代码][代码] [代码][代码]fail: (err) => {[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]status: [代码][代码]'warn'[代码][代码],[代码][代码] [代码][代码]connecting: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]hintLine1: [代码][代码]'登录失败'[代码][代码],[代码][代码] [代码][代码]hintLine2: err.message || err[代码][代码] [代码][代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 监听 WebSocket 事件[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]listen() {[代码][代码] [代码][代码]wx.onSocketOpen(() => {[代码][代码] [代码][代码]this[代码][代码].socketOpen = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]status: [代码][代码]'success'[代码][代码],[代码][代码] [代码][代码]connecting: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]hintLine1: [代码][代码]'连接成功'[代码][代码],[代码][代码] [代码][代码]hintLine2: [代码][代码]'现在可以通过 WebSocket 发送接收消息了'[代码][代码] [代码][代码]});[代码][代码] [代码][代码]console.info([代码][代码]'WebSocket 已连接'[代码][代码]);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]wx.onSocketMessage((message) => {[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]hintLine2: message.data[代码][代码] [代码][代码]});[代码][代码] [代码][代码]lab.finish([代码][代码]'websocket'[代码][代码]);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]wx.onSocketClose(() => {[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]status: [代码][代码]'waiting'[代码][代码],[代码][代码] [代码][代码]hintLine1: [代码][代码]'WebSocket 已关闭'[代码][代码] [代码][代码]});[代码][代码] [代码][代码]console.info([代码][代码]'WebSocket 已关闭'[代码][代码]);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]wx.onSocketError(() => {[代码][代码] [代码][代码]setTimeout(() => {[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]status: [代码][代码]'warn'[代码][代码],[代码][代码] [代码][代码]connecting: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]hintLine1: [代码][代码]'发生错误'[代码][代码],[代码][代码] [代码][代码]hintLine2: [代码][代码]'WebSocket 连接建立失败'[代码][代码] [代码][代码]});[代码][代码] [代码][代码]});[代码][代码] [代码][代码]console.error([代码][代码]'WebSocket 错误'[代码][代码]);[代码][代码] [代码][代码]});[代码][代码] [代码][代码]},[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 发送一个包含当前时间信息的消息[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]send() {[代码][代码] [代码][代码]wx.sendSocketMessage({[代码][代码] [代码][代码]data: [代码][代码]new[代码] [代码]Date().toTimeString().split([代码][代码]' '[代码][代码]).shift() + [代码][代码]'.'[代码] [代码]+ ([代码][代码]new[代码] [代码]Date().getMilliseconds())[代码][代码] [代码][代码]});[代码][代码] [代码][代码]},[代码][代码] [代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 关闭 WebSocket 连接[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]close() {[代码][代码] [代码][代码]this[代码][代码].socketOpen = [代码][代码]false[代码][代码];[代码][代码] [代码][代码]wx.closeSocket();[代码][代码] [代码][代码]}[代码][代码]});[代码]
如何基于centos(或者ubuntu)搭建微信小程序服务器?大家好: 我基于微信官方网址上的指导文档 (https://cloud.tencent.com/developer/labs/lab/10004)在centos上搭建微信小程序服务器。 我的https域名解析都成功了,但是就是小程序会话总提示“请检查网络状态”。 想请教大家这是啥子原因呀? 客户端代码在附件中,是官方网站上下载下来的。 [图片] [图片][图片]
2019-09-04