Skip to content

Commit daad741

Browse files
committed
updated websocket library with some checks and fixed createcharfromint
1 parent 7be3bab commit daad741

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

Diff for: typescript/yarp-ws.ts

+29-17
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,10 @@ function createBottleFromString(data:string)
205205
// deprecated, need to switch to view
206206
function createcharfromint(num:number)
207207
{
208-
var char1 = num %256
209-
num = num /256
210-
var char2 = num %256
211-
num = num /256
212-
var char3 = num %256
213-
num = num /256
214-
var char4 = num %256
215-
return String.fromCharCode(char1,char2,char3,char4)
208+
var arr = new ArrayBuffer(4); // an Int32 takes 4 bytes
209+
var view = new DataView(arr);
210+
view.setUint32(0, num, true); // byteOffset = 0; litteEndian = true
211+
return convertArrayBufferToString(arr)
216212
}
217213

218214
// closes a connection, since the connection must be closed from the server, it sends a message to the server to close the connection
@@ -240,9 +236,9 @@ function convertArrayBufferToString(buffer:ArrayBuffer){
240236
}
241237

242238
// it prints the received object on the console
243-
function printBuffer(buffer:ArrayBuffer){
244-
245-
if (buffer.byteLength > 8){
239+
function printBuffer(buffer:ArrayBuffer)
240+
{
241+
if (buffer.byteLength > 8) {
246242
console.log("handling bottle")
247243
console.log(handleBottle(buffer))
248244
} else {
@@ -292,8 +288,12 @@ function checkIfPortExistsAndConnect(buffer:ArrayBuffer){
292288
var url = "ws://" + ip + ":" + port + "?ws"
293289
console.log("the url to connect to is: " + url);
294290
var newWebsocket = connectToYarp(url);
295-
newWebsocket.onmessage = logMessage
296-
revertConnection(newWebsocket)
291+
if (newWebsocket) {
292+
newWebsocket.onmessage = logMessage
293+
revertConnection(newWebsocket)
294+
} else {
295+
console.error("newWebsocket: " + url + "is undefined, check previous error")
296+
}
297297
return newWebsocket
298298
}
299299

@@ -305,7 +305,12 @@ function handleAddressResponse(data:any){
305305

306306
// connects to yarp with the given url (TODO FIXME STE maybe it can be done only with ip and port?)
307307
function connectToYarp(url:string){
308-
var websocket = new WebSocket(url);
308+
var websocket
309+
try {
310+
websocket = new WebSocket(url);
311+
} catch (error) {
312+
console.error("websocket client: " + url + " - " + error);
313+
}
309314
return websocket
310315
}
311316

@@ -346,7 +351,6 @@ function setupNewConnectionToPort(websocket: WebSocket, portName: string, closeW
346351
closeConnection(websocket)
347352
}
348353
}
349-
console.log("sending message")
350354
websocket.onmessage = handler
351355
sendQueryMessage(websocket, portName)
352356
})
@@ -355,10 +359,18 @@ function setupNewConnectionToPort(websocket: WebSocket, portName: string, closeW
355359

356360
function setupNewConnectionToPortWithAddress(rootip:string, rootport: number, portName: string){
357361
var websocket = connectToYarp("ws://" +rootip + ":" + rootport+ "?ws")
358-
return setupNewConnectionToPort(websocket, portName, true)
362+
if (websocket) {
363+
return setupNewConnectionToPort(websocket, portName, true)
364+
} else {
365+
console.error("cannot connect to websocket " + portName)
366+
}
359367
}
360368

361369
function getPortAndIpWithAddress(rootip:string, rootport: number, portName: string){
362370
var websocket = connectToYarp("ws://" +rootip + ":" + rootport+ "?ws")
363-
return getPortAndIp(websocket,portName, true)
371+
if (websocket) {
372+
return getPortAndIp(websocket,portName, true)
373+
} else {
374+
console.error("cannot connect to websocket " + portName)
375+
}
364376
}

0 commit comments

Comments
 (0)