10-22-2017 09:41 - edited 10-22-2017 09:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-22-2017 09:41 - edited 10-22-2017 09:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
As there is no file transfer API from app to companion, I test this code using messaging API.
The code works but with no buffer protection and buffer resetting (actually I don't know how, my MAX_MESSAGE_SIZE reported 1027 instead of 1024) .
And if conection lost during transfer, sending should be triggered manually cause I don't know if there is a kind of call as "send()"
I hope someone can correct me or add some advice, thank you!
import * as messaging from "messaging"; import { peerSocket, MessageSocket } from "messaging";
var stringArray = []; var stringReady = false; var count = 0; var many = 0; function manualStart() { console.log("ionic:Max message size=" + MessageSocket.MAX_MESSAGE_SIZE); //manual reset to stop and restart var count = 0; //let testString = fs.readFileSync("testText.txt", "UTF8"); let testString = "START..."; many = Math.floor(testString.length / 64) + 1; console.log("many = "+many); for (var i = 0; i <= many -1; i++) { stringArray[i] = testString.substring(i*64, i*64+64); console.log("array"+"-"+i+" = "+stringArray[i]); } //string is ready, go stringReady = true; continueSendingData(); } //Listen for the onbufferedamountdecrease event messaging.peerSocket.onbufferedamountdecrease = function() { console.log("stringReady = "+stringReady+" count = "+count); if ((stringReady)&&(count <= many)){ continueSendingData(); } } //real sending function continueSendingData() { console.log("sending No: " + count); // Send data only while the buffer contains less than 64 bytes while (messaging.peerSocket.bufferedAmount < 64) { let subS = stringArray[count]; if (subS != null){ messaging.peerSocket.send(subS); } count += 1; if (count > many){stringReady = false;} } } //Message socket opens messaging.peerSocket.onopen = function() { console.log("Ionic Socket Open"); } //Message socket closes messaging.peerSocket.close = function() { console.log("Companion Socket Closed"); } //Listen for the onerror event messaging.peerSocket.onerror = function(err) { console.log("Ionic Side: connection error: " + err.code + " - " + err.message); }
Answered! Go to the Best Answer.
Accepted Solutions
10-25-2017 10:50
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-25-2017 10:50
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I finally finished the debug of the code myself, now the code is steady and strong, the ONLY modification is this line:
if ((stringReady)&&(count <= many-1))
Yes, just that silly issue without "-1" made the socket unstable... because you pushed an empty to it!
and I am making this reply as the final solution, hope it helps for your project.
Good luck!
10-25-2017 10:50
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

10-25-2017 10:50
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I finally finished the debug of the code myself, now the code is steady and strong, the ONLY modification is this line:
if ((stringReady)&&(count <= many-1))
Yes, just that silly issue without "-1" made the socket unstable... because you pushed an empty to it!
and I am making this reply as the final solution, hope it helps for your project.
Good luck!
08-30-2018 20:30
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-30-2018 20:30
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Glad to see that someone has the same problem that I have.
Do you happen to still have the Companion App code of that?

