webfast 0.1.35 → 0.1.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,34 @@
1
1
  console.log(`WebFast!`);
2
- window.WebFast = {
3
- ts : Date.now()
2
+ let web= {};
3
+ web.fast = {
4
+ ts : Date.now(),
5
+ action : function(data,ell) {
6
+ console.log(`Action Function`,data,ell);
7
+ },
8
+ functions : {
9
+ form : function(data,ell) {
10
+ console.log(`Handle Form Function`);
11
+ }
12
+ },
13
+ que : {
14
+ list : [],
15
+ state : false,
16
+ run : function(count){
17
+ // Grab first before sending
18
+ item = web.fast.que.list[0];
19
+ if (item != undefined) {
20
+ delete web.fast.que.list[0];
21
+ web.fast.que.list.slice(1);
22
+ console.log(`Sending Item`,item);
23
+
24
+ web.fast.socket.send(JSON.stringify(item)); // Send data to the server
25
+ }
26
+ }
27
+ },
28
+ tmp : {
29
+ int : {}
30
+ },
31
+ socket : undefined
4
32
  }
5
33
  // Connect to the Socket.IO server
6
34
  const telegram = window.Telegram.WebApp;
@@ -18,25 +46,129 @@ if (telegram.initData == ``) {
18
46
 
19
47
  const socketURL = `wss://${webfastSocket.replace(`https://`,``)}socket.io/?qbt=${setData}`;
20
48
 
21
- try {
22
- new WebSocket(socketURL,`test`,{
23
- timeout : 0
24
- });
49
+ // Assuming you have a variable `socketURL` containing your WebSocket URL
50
+ web.fast.connectWebSocket = function(socketURL,maxRetries = 40, retries = 0) {
51
+ web.fast.createWebSocket = function(socketURL, maxRetries, retries) {
52
+ const ws = new WebSocket(socketURL);
53
+ web.fast.socket = ws;
54
+ ws.onopen = () => {
55
+ console.log('WebSocket connected');
56
+ // Start other things (e.g., send initial data)
57
+ web.fast.que.state = true;
58
+ };
59
+
60
+ ws.onmessage = (event) => {
61
+ console.log('Received:', event.data);
62
+ // Handle received data
63
+ web.fast.que.state = Date.now();
64
+ web.fast.receive(`socket`,event.data); // Placeholder for processing response
65
+ };
66
+
67
+ ws.onclose = (event) => {
68
+ web.fast.que.state = false;
69
+ if (retries < maxRetries) {
70
+ retries++;
71
+ console.log(`WebSocket closed. Retrying in 10 seconds...`);
72
+ setTimeout(web.fast.createWebSocket(socketURL,maxRetries,retries), 10000); // Retry after 10 seconds
73
+ } else {
74
+ console.log(`WebSocket connection failed after ${maxRetries} attempts.`);
75
+ // Handle failure (e.g., show an error message)
76
+ }
77
+ };
78
+
79
+ ws.onerror = (error) => {
80
+ console.error('WebSocket error:', error);
81
+ // Handle error (e.g., show an error message)
82
+ web.fast.que.state = false;
83
+ };
84
+
85
+ // Send message to path for example api.example.event
86
+ web.fast.sendMessage = function(path,message) {
87
+ // Add to que
88
+ web.fast.que.list.push({
89
+ path : path,
90
+ message : message});
91
+ }
92
+ };
93
+
94
+ web.fast.createWebSocket(socketURL, maxRetries, retries); // Initial connection
95
+ }
25
96
 
26
- // Start the other things
27
- //alert(telegram.initData);
28
- } catch (err) {
29
- alert(`Err`);
30
- console.error(err);
97
+ // Call the function to start the WebSocket connection
98
+ web.fast.connectWebSocket(socketURL);
99
+ web.fast.receive = function(data) {
100
+ // Placeholder for processing the received data
101
+ // Implement your logic here (e.g., update UI, handle specific messages)
102
+ console.log('Processing received data:', data);
31
103
  }
32
104
 
33
105
  // On Ready desable all forms with jquery
34
106
  jQuery(document).ready(function() {
35
- jQuery('form').submit(function(e) {
36
- e.preventDefault(); // Prevent the default form submission
107
+
108
+ // Make function
109
+ web.fast.tmp.int.que = setInterval(function(){
110
+ // This is the function
111
+ if (web.fast.que.state != false && web.fast.que.list.length > 0) {
112
+ // State check
113
+ web.fast.que.run();
114
+ }
115
+ },10);
116
+
117
+ // Scan for all webfast ellements
118
+ jQuery(`[webfast]`).each(function() {
119
+ let elementType = jQuery(this).prop('nodeName');
120
+ console.log('Element type:', elementType);
121
+
122
+ // Get now the data
123
+ let webAction = jQuery(this).attr(`webfast`);
124
+ let action = jQuery(this).attr(`webfast-${webAction}`);
125
+ console.log(`Data Actions webAction, action`,webAction,action);
126
+
127
+ // Create action runner
128
+ // Check if id
129
+ if (jQuery(this).attr(`id`) == undefined) {
130
+ const array = new Uint32Array(1);
131
+ window.crypto.getRandomValues(array);
132
+ const randomValue = array[0];
133
+
134
+ console.log(`Set UUID: ${randomValue}`);
135
+ jQuery(this).attr(`id`,randomValue);
136
+ }
37
137
 
38
- // Check what to do like socketpath
39
- alert(`Form Submit`);
40
- return false;
138
+ switch (elementType) {
139
+ case "FORM":
140
+ jQuery(this).submit(function(e) {
141
+ e.preventDefault(); // Prevent the default form submission
142
+
143
+ // Check what to do like socketpath
144
+ web.fast.action(action,this,e);
145
+ return false;
146
+ });
147
+ break;
148
+ case "DIV":
149
+ // We will do the div element part
150
+ // check whyt type etc
151
+ // Make request to server with websocket thingy
152
+ // Set in QUE
153
+ let other;
154
+ if (action == `list`) {
155
+ other = {
156
+ type : action,
157
+ html : jQuery(this).html()
158
+ }
159
+ }
160
+ web.fast.sendMessage(`socket.api.${webAction}`,{
161
+ ts : Date.now(),
162
+ ell : jQuery(this).attr(`id`),
163
+ other : other
164
+ })
165
+ break;
166
+ case "BUTTON":
167
+ // Check button interaction
168
+ console.error(`SETUP BUTTON INTERACTION`);
169
+ break;
170
+ default:
171
+ console.error(`No Action for : ${elementType}`);
172
+ }
41
173
  });
42
174
  });
package/example.js CHANGED
@@ -1,5 +1,8 @@
1
1
  const path = require(`path`);
2
2
  let program = require(path.join(__dirname,`index.js`))({
3
- wget : '/usr/local/bin/wget'
3
+ wget : '/usr/local/bin/wget',
4
+ process : {
5
+ ts : Date.now()
6
+ }
4
7
  });
5
8
  console.log(`Required`);
@@ -5,6 +5,10 @@ module.exports = async function(req,res,body,params,command,middleValue) {
5
5
  if (middleValue.location != undefined) {
6
6
  locSendMessage = `Thank you for sending your location\n ${middleValue.location.longitude}\n${middleValue.location.latitude}`;
7
7
  }
8
+
9
+ const scripting = await program.modules.telegram.script.function.check(program,command,middleValue.chat.id,middleValue,body);
10
+ console.log(scripting);
11
+
8
12
  return {
9
13
  message : locSendMessage,
10
14
  response : {
@@ -49,11 +49,15 @@ module.exports = async function(program,req,res,body,params,command,middleValue)
49
49
  console.log(scripting);
50
50
 
51
51
  // Send back
52
- return {
53
- message : locSendMessage,
54
- response : {
52
+ if (scripting == true) {
53
+ return true;
54
+ } else {
55
+ return {
55
56
  message : locSendMessage,
56
- uuid : uuid
57
+ response : {
58
+ message : locSendMessage,
59
+ uuid : uuid
60
+ }
57
61
  }
58
62
  }
59
63
  }
@@ -34,6 +34,7 @@ module.exports = {
34
34
  } catch(err) {
35
35
  console.error(err);
36
36
  console.error(`Error Scripting`);
37
+ return false;
37
38
  }
38
39
  },
39
40
  response : async function() {
@@ -2,6 +2,7 @@ module.exports = async function (program) {
2
2
  console.log(`Starting UP Express`);
3
3
  program.express = {
4
4
  ts: Date.now(),
5
+ process : program.set.process
5
6
  };
6
7
 
7
8
  const express = require('express');
@@ -267,16 +268,46 @@ module.exports = async function (program) {
267
268
  });
268
269
 
269
270
  // WebSocket on message event
270
- ws.on('message', (message) => {
271
+ ws.on('message', async (message) => {
271
272
  console.log(`Received message from ${clientId}: ${message}`);
272
273
 
273
- // Add your custom on message logic here
274
- // For example, you can broadcast the message to all connected clients
275
- clients.forEach((client, id) => {
276
- if (client.readyState === WebSocket.OPEN && id !== clientId) {
277
- client.send(`Broadcast from ${clientId}: ${message}`);
274
+ try {
275
+ // Check for function
276
+ const json = JSON.parse(message.toString(`utf-8`));
277
+ const data = json.message;
278
+ const path = json.path;
279
+ const split = path.split(".")
280
+
281
+ // Check if function is running in program modules that you can add in the init scirpt when using remote
282
+ if (program.express.process != undefined) {
283
+ try {
284
+ let resp = await program.express.process[split[0]][split[1]][split[2]](program,ws,json,data,path);
285
+ if (resp != false) {
286
+ ws.send(JSON.stringify(resp));
287
+ }
288
+ } catch (err) {
289
+ console.error(`Error Running program.express.process for `,split[0],split[1],split[2]);
290
+ ws.send(JSON.stringify({
291
+ ts : Date.now(),
292
+ error : true,
293
+ message : `Error Event Program receive`
294
+ }));
295
+ }
278
296
  }
279
- });
297
+
298
+ // Add your custom on message logic here
299
+ // For example, you can broadcast the message to all connected clients
300
+ clients.forEach((client, id) => {
301
+ if (client.readyState === WebSocket.OPEN && id !== clientId) {
302
+ //ws.send(`Broadcast from ${clientId}: ${message}`);
303
+ }
304
+ });
305
+
306
+ // Check if
307
+ } catch(err) {
308
+ console.error(`Error Something`);
309
+
310
+ }
280
311
  });
281
312
  });
282
313
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webfast",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "description": "WebFast! Bot Application, including TON mini-apps for makign it easy and fast to build ini-apps",
5
5
  "main": "index.js",
6
6
  "repository": {