最近在研究微信小程序的开发,中间遇到了不少问题,趟了不少坑,这里和大家分享一下我的一些经验,希望能给大家提供一些帮助。
import * as websocket from "websocket";
import * as http from "http";
const WebSocketServer: typeof websocket.server = websocket.server;
// 创建一个http Server
let httpServer: http.Server = http.createServer((request, response) => {
console.log("received a request");
response.writeHead(404);
response.end();
});
// 创建一个websocket Server,websocket Server需要建立在http server之上
let wsServer: websocket.server = new WebSocketServer({
httpServer: httpServer,
autoAcceptConnections: true
});
// 事件监听
wsServer.on("connect", (connection) => {
console.log(">>>come from: " + connection.remoteAddress); // 显示连接客户端的ip地址
connection.on("message", (message) => {
console.log(message.type);
console.log(">>>message: ", message); // 接收到信息的类型和内容,注意都是utf8编码
connection.sendUTF(message.utf8Data); // 把接收到的信息发回去
});
connection.on("close", (reasonCode, description) => {
console.log(connection.remoteAddress + " has disconnected.");
});
});
// 启动服务器
httpServer.listen(80, () => {
console.log(">>>Http Server is listening on port 80!");
});
onLoad: function () {
this.setData({
});
var that = this;
wx.connectSocket({
url: 'ws://xxx.xxx.com' // 这里是服务器的地址
}),
wx.onSocketOpen(function (res) {
// callback
console.log("WebSocket连接已打开!"); // 打开WebSocket连接,在进行通信之前必须先打开一个连接
}),
wx.onSocketError(function (res) { // WebSocket错误监听
console.log('WebSocket连接打开失败,请检查!')
}),
wx.onSocketMessage(function (msg) { // WebSocket数据接收监听
// CallBack
console.log(msg);
}),
wx.onSocketClose(function () { // WebSocket关闭监听
// callback
console.log('WebSocket服务器已经关闭!');
}),
wx.sendSocketMessage({ // 向服务器发送数据,注意这个方法之必须在调用wx.connectSocket和wx.onSocketOpen回调之后
data: "string" // 官方文档里data可以是string或者ArrayBuffer,但是注意这个ArrayBuffer并不是Array
});
}
import * as websocket from "websocket";
import * as https from "https";
import * as fs from "fs"; // 需要引用node.js的fs来读取三个SSL证书文件
const WebSocketServer: typeof websocket.server = websocket.server;
// https Server
let options: any = {
key: fs.readFileSync("./key/private.key", "utf8"), // 这里设置三个证书的位置,记得指明“utf8”编码,key、cert、ca三个属性必须都有且对应三个文件
cert: fs.readFileSync("./key/certificate.crt", "utf8"),
ca: fs.readFileSync("./key/ca_bundle.crt", "utf8")
};
let httpsServer: any = https.createServer(options, (request, response) => { // 注意这里已经是https了
console.log("received a request");
response.writeHead(200);
response.end("hello world\n");
});
// websocket Server
let wsServer: websocket.server = new WebSocketServer({
httpServer: httpsServer, // 对应的这里的httpServer后面也应该是对应的httpsSrver(名称无所谓但是本体应该是httpsServer)
autoAcceptConnections: true
});
// 事件监听
wsServer.on("connect", (connection) => {
console.log(">>>come from: " + connection.remoteAddress);
connection.on("message", (message) => {
console.log(message.type);
console.log(">>>message: ", message);
connection.sendUTF(message.utf8Data); // 把接收到的信息发回去
});
connection.on("close", (reasonCode, description) => {
console.log(connection.remoteAddress + " has disconnected.");
});
});
// 启动服务器
httpsServer.listen(443, () => { // 端口
console.log(">>>Https Server is listening on port 443!");
});