@@ -205,14 +205,10 @@ function createBottleFromString(data:string)
205
205
// deprecated, need to switch to view
206
206
function createcharfromint ( num :number )
207
207
{
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 )
216
212
}
217
213
218
214
// 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){
240
236
}
241
237
242
238
// 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 ) {
246
242
console . log ( "handling bottle" )
247
243
console . log ( handleBottle ( buffer ) )
248
244
} else {
@@ -292,8 +288,12 @@ function checkIfPortExistsAndConnect(buffer:ArrayBuffer){
292
288
var url = "ws://" + ip + ":" + port + "?ws"
293
289
console . log ( "the url to connect to is: " + url ) ;
294
290
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
+ }
297
297
return newWebsocket
298
298
}
299
299
@@ -305,7 +305,12 @@ function handleAddressResponse(data:any){
305
305
306
306
// connects to yarp with the given url (TODO FIXME STE maybe it can be done only with ip and port?)
307
307
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
+ }
309
314
return websocket
310
315
}
311
316
@@ -346,7 +351,6 @@ function setupNewConnectionToPort(websocket: WebSocket, portName: string, closeW
346
351
closeConnection ( websocket )
347
352
}
348
353
}
349
- console . log ( "sending message" )
350
354
websocket . onmessage = handler
351
355
sendQueryMessage ( websocket , portName )
352
356
} )
@@ -355,10 +359,18 @@ function setupNewConnectionToPort(websocket: WebSocket, portName: string, closeW
355
359
356
360
function setupNewConnectionToPortWithAddress ( rootip :string , rootport : number , portName : string ) {
357
361
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
+ }
359
367
}
360
368
361
369
function getPortAndIpWithAddress ( rootip :string , rootport : number , portName : string ) {
362
370
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
+ }
364
376
}
0 commit comments