Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: emoji crash
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie committed Aug 3, 2018
1 parent 3b35f19 commit 6d16fc5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
10 changes: 10 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Pomelo = require('./lib/Pomelo');
// const WebSocket = require('ws');

function wsCreator({url, onError, onOpen, onMessage, onClose}) {
const ws = wx.connectSocket({url:url});
Expand All @@ -9,6 +10,15 @@ function wsCreator({url, onError, onOpen, onMessage, onClose}) {
return ws;
}

// function wsCreator({url, onError, onOpen, onMessage, onClose}) {
// const ws = new WebSocket(url);
// ws.onerror = onError;
// ws.onopen = onOpen;
// ws.onmessage = onMessage;
// ws.onclose = onClose;
// return ws;
// }

function urlGenerator(host, port) {
let url = 'wss://' + host;
if (port) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pomelo-weixin-client",
"version": "1.3.0",
"version": "1.4.0",
"description": "pomelo微信小程序客户端",
"main": "index.js",
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/Pomelo.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ module.exports = class Pomelo extends EventEmitter {
}
send(packet) {
this.socket.send({ data: packet.buffer });
// this.socket.send(packet.buffer);
}
onData(msg) {
msg = this.decode(msg);
Expand Down
39 changes: 22 additions & 17 deletions src/Protocal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {copyArray} = require('./util');
// const {TextDecoder} = require('text-encoding');

module.exports = class Protocol {
/**
Expand Down Expand Up @@ -37,25 +38,29 @@ module.exports = class Protocol {
* return Message Object
*/
static strdecode(buffer) {

var bytes = new Uint8Array(buffer);
var array = [];
var offset = 0;
var charCode = 0;
var end = bytes.length;
while (offset < end) {
if (bytes[offset] < 128) {
charCode = bytes[offset];
offset += 1;
} else if (bytes[offset] < 224) {
charCode = ((bytes[offset] & 0x3f) << 6) + (bytes[offset + 1] & 0x3f);
offset += 2;
} else {
charCode = ((bytes[offset] & 0x0f) << 12) + ((bytes[offset + 1] & 0x3f) << 6) + (bytes[offset + 2] & 0x3f);
offset += 3;
if (wx && typeof wx.arrayBufferToBase64 === 'function') {
var base64 = wx.arrayBufferToBase64(bytes);
return atob(base64);
} else {
var array = [];
var offset = 0;
var charCode = 0;
var end = bytes.length;
while (offset < end) {
if (bytes[offset] < 128) {
charCode = bytes[offset];
offset += 1;
} else if (bytes[offset] < 224) {
charCode = ((bytes[offset] & 0x3f) << 6) + (bytes[offset + 1] & 0x3f);
offset += 2;
} else {
charCode = ((bytes[offset] & 0x0f) << 12) + ((bytes[offset + 1] & 0x3f) << 6) + (bytes[offset + 2] & 0x3f);
offset += 3;
}
array.push(charCode);
}
array.push(charCode);
return String.fromCharCode.apply(null, array);
}
return String.fromCharCode.apply(null, array);
};
}

0 comments on commit 6d16fc5

Please sign in to comment.