Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jan 3, 2024
1 parent b4bfa71 commit 46c8f74
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 93 deletions.
62 changes: 17 additions & 45 deletions src/push-uniapp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

function Push(options) {
this.doNotConnect = 0;
options = options || {};
Expand All @@ -15,7 +14,9 @@ function Push(options) {

Push.prototype.checkoutPing = function() {
var _this = this;
setTimeout(function () {
_this.checkoutPingTimer && clearTimeout(_this.checkoutPingTimer);
_this.checkoutPingTimer = setTimeout(function () {
_this.checkoutPingTimer = 0;
if (_this.connection.state === 'connected') {
_this.connection.send('{"event":"pusher:ping","data":{}}');
if (_this.pingTimeoutTimer) {
Expand Down Expand Up @@ -62,7 +63,7 @@ Push.prototype.createConnection = function () {
_this.pingTimeoutTimer = 0;
}

params = paramsDataParse(params.data);
params = JSON.parse(params.data);
var event = params.event;
var channel_name = params.channel;

Expand All @@ -73,7 +74,7 @@ Push.prototype.createConnection = function () {
if (event === 'pusher:error') {
throw Error(params.data.message);
}
var data = paramsDataParse(params.data), channel;
var data = JSON.parse(params.data), channel;
if (event === 'pusher_internal:subscription_succeeded') {
channel = _this.channels[channel_name];
channel.subscribed = true;
Expand All @@ -83,7 +84,7 @@ Push.prototype.createConnection = function () {
}
if (event === 'pusher:connection_established') {
_this.connection.socket_id = data.socket_id;
_this.connection.state = 'connected';
_this.connection.updateNetworkState('connected');
_this.subscribeAll();
}
if (event.indexOf('pusher_internal') !== -1) {
Expand Down Expand Up @@ -158,6 +159,7 @@ function createChannel(channel_name, push)
channel.subscribeCb = function () {
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
}
channel.processSubscribe();
return channel;
}

Expand Down Expand Up @@ -186,43 +188,20 @@ function createPrivateChannel(channel_name, push)

function createPresenceChannel(channel_name, push)
{
var channel = new Channel(push.connection, channel_name);
push.channels[channel_name] = channel;
channel.subscribeCb = function () {
__ajax({
url: push.config.auth,
type: 'POST',
data: {
channel_name: channel_name,
socket_id: push.connection.socket_id,
channel_data: push.config.channel_data ? JSON.stringify(push.config.channel_data) : null
},
success: function (data) {
data = JSON.parse(data);
data.channel = channel_name;
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:data}));
},
error: function (e) {
throw Error(e);
}
});
};
channel.processSubscribe();
return channel;
// return createPrivateChannel(channel_name, push);
return createPrivateChannel(channel_name, push);
}

/*window.addEventListener('online', function(){
var con;
for (var i in Push.instances) {
con = Push.instances[i].connection;
con.reconnectInterval = 1;
if (con.state === 'connecting') {
con.connect();
uni.onNetworkStatusChange(function (res) {
if(res.isConnected) {
for (var i in Push.instances) {
con = Push.instances[i].connection;
con.reconnectInterval = 1;
if (con.state === 'connecting') {
con.connect();
}
}
}
});*/

});

function Connection(options) {
this.dispatcher = new Dispatcher();
Expand Down Expand Up @@ -844,11 +823,4 @@ function formatParams(data){
return arr.join('&');
}

function paramsDataParse(data){
if(typeof (data) === 'string'){
return JSON.parse(data);
}
return data
}

export default Push
68 changes: 20 additions & 48 deletions src/push.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

function Push(options) {
this.doNotConnect = 0;
options = options || {};
Expand All @@ -15,7 +14,9 @@ function Push(options) {

Push.prototype.checkoutPing = function() {
var _this = this;
setTimeout(function () {
_this.checkoutPingTimer && clearTimeout(_this.checkoutPingTimer);
_this.checkoutPingTimer = setTimeout(function () {
_this.checkoutPingTimer = 0;
if (_this.connection.state === 'connected') {
_this.connection.send('{"event":"pusher:ping","data":{}}');
if (_this.pingTimeoutTimer) {
Expand Down Expand Up @@ -61,19 +62,19 @@ Push.prototype.createConnection = function () {
clearTimeout(_this.pingTimeoutTimer);
_this.pingTimeoutTimer = 0;
}
params = paramsDataParse(params.data);

params = JSON.parse(params.data);
var event = params.event;
var channel_name = params.channel;

if (event === 'pusher:pong') {
_this.checkoutPing();
return;
}
if (event === 'pusher:error') {
throw Error(params.data.message);
}
var data = paramsDataParse(params.data), channel;
var data = JSON.parse(params.data), channel;
if (event === 'pusher_internal:subscription_succeeded') {
channel = _this.channels[channel_name];
channel.subscribed = true;
Expand All @@ -83,7 +84,7 @@ Push.prototype.createConnection = function () {
}
if (event === 'pusher:connection_established') {
_this.connection.socket_id = data.socket_id;
_this.connection.state = 'connected';
_this.connection.updateNetworkState('connected');
_this.subscribeAll();
}
if (event.indexOf('pusher_internal') !== -1) {
Expand Down Expand Up @@ -158,6 +159,7 @@ function createChannel(channel_name, push)
channel.subscribeCb = function () {
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
}
channel.processSubscribe();
return channel;
}

Expand Down Expand Up @@ -186,30 +188,7 @@ function createPrivateChannel(channel_name, push)

function createPresenceChannel(channel_name, push)
{
var channel = new Channel(push.connection, channel_name);
push.channels[channel_name] = channel;
channel.subscribeCb = function () {
__ajax({
url: push.config.auth,
type: 'POST',
data: {
channel_name: channel_name,
socket_id: push.connection.socket_id,
channel_data: push.config.channel_data ? JSON.stringify(push.config.channel_data) : null
},
success: function (data) {
data = JSON.parse(data);
data.channel = channel_name;
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:data}));
},
error: function (e) {
throw Error(e);
}
});
};
channel.processSubscribe();
return channel;
// return createPrivateChannel(channel_name, push);
return createPrivateChannel(channel_name, push);
}

/*window.addEventListener('online', function(){
Expand Down Expand Up @@ -364,16 +343,16 @@ var __extends = (this && this.__extends) || function (d, b) {

function Channel(connection, channel_name) {
this.subscribed = false;
this.dispatcher = new Dispatcher();
this.dispatcher = new Dispatcher();
this.connection = connection;
this.channelName = channel_name;
this.subscribeCb = null;
this.queue = [];
__extends(this, this.dispatcher);
var properies = ['on', 'off', 'emit'];
for (var i in properies) {
__extends(this, this.dispatcher);
var properies = ['on', 'off', 'emit'];
for (var i in properies) {
this[properies[i]] = this.dispatcher[properies[i]];
}
}
}

Channel.prototype.processSubscribe = function () {
Expand Down Expand Up @@ -406,7 +385,7 @@ Channel.prototype.trigger = function (event, data) {

////////////////
var Collections = (function () {
var exports = {};
var exports = {};
function extend(target) {
var sources = [];
for (var _i = 1; _i < arguments.length; _i++) {
Expand Down Expand Up @@ -513,8 +492,8 @@ var Collections = (function () {
exports.mapObject = mapObject;
function filter(array, test) {
test = test || function (value) {
return !!value;
};
return !!value;
};
var result = [];
for (var i = 0; i < array.length; i++) {
if (test(array[i], i, array, result)) {
Expand Down Expand Up @@ -758,16 +737,9 @@ function __ajax(options){
}

function formatParams(data){
var arr = [];
for(let name in data){
var arr=[];
for(var name in data){
arr.push(encodeURIComponent(name)+'='+encodeURIComponent(data[name]));
}
return arr.join('&');
}

function paramsDataParse(data){
if(typeof (data) === 'string'){
return JSON.parse(data);
}
return data
}

0 comments on commit 46c8f74

Please sign in to comment.