Jump to content
The simFlight Network Forums

fsuipc client vs lua


FSEPIC

Recommended Posts

Hi , 

on 1 :   went on bike trip , because it was driving me nuts . On the way home i came to the same conclusion,  because the console .log did not show up 

on 2.  i now start from:  node   "anyjsfile".js ,    and start the  index.html  from chrome or the live server in VS Code, and the url is :  http://127.0.0.1:5500/index.html  .

I get connection now , but now my code  is a bit messy imho ; -)   

 

A:   

I can NOT  find a way to start the code in the connection function .   

From : https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/#6-one-more-thing-new-function  , i understand this is 1 of 6 ways to declare a function in JS.   

So i  defined a "standard"  function in  test.js and just added  a few lines:

// test.js

function myfunction()  
 {
  
var ws = null;
   ws = new WebSocket('ws://localhost:2048/fsuipc/'"fsuipc");
    openConnection();
 
 
-----------------------
this.start = function () {
    };
    
    this.stop = function () {
        if (ws) {
            ws.close();
        }
    };
 
    var clearMessages = function () {
        document.getElementById("connectionStatus").innerText = "";
        document.getElementById("connectionError").innerText = "";
    };
 
    this.openConnection = function () {
        if (!ws) {
 
            clearMessages();
 
            // Create a new WebSocket. 
            // The server url is from the text box on the form
            //var serverURL = document.getElementById("serverURL").value;
 
            
 
            ws = new WebSocket('ws://localhost:2048/fsuipc/'"fsuipc");
           
            // Handle the onopen event
            ws.onopen = function () {
                console.log("open");
                document.getElementById("connectionStatus").innerText = "Connection Open";
            };
 
            // Handle the onclose event
            ws.onclose = function () {
                document.getElementById("connectionStatus").innerText = "Connection Closed";
                // clear the WebSocket so we can try again
                ws = null;
            };
 
            // Handle the onerror event
            ws.onerror = function () {
                console.log("error");
 
                document.getElementById("connectionError").innerText = "WebSocket Error";
            };
        }
    }
 
    this.closeConnection = function () {
        if (ws) {
            clearMessages();
            ws.close();
 
        }
    }
 
);
  };

 

 

B . 

I call this from a button indeed:

  <input type = "button" onclick = "myfunction()" value = "openwebsocket">
and  the link in the header
<script type = "text/javascript" src="test.js"></script>  

 

C. i do have connection now , but  it sure  is ugly , not your fault ,  but i would like to be able to refactor the code  ,    ,   ( so i can e.g. bring the dictionaries back into JS   , as they are well supported in JS. ) .

D:  i tried :

<input type = "button" onclick = "connection()" value = "openwebsocket"> 

<input type = "button" onclick = "connection.openconnetion()" value = "openwebsocket"> 

and 10 more ways , but no no avail ;-)  

(my guess is your code uses some framework with routing ???)

 

 

 

 

Link to comment
Share on other sites

Quote

(my guess is your code uses some framework with routing ???)

No, I deliberately avoided any third party frameworks and libraries, on both the server and browser.

Quote

function myfunction()  

If you declare a function like this you need to instantiate it with the new keyword.

var myFunctionInstance = new myFunction();

and then use the instance, not the function definition.. 

In my original example I use a shortcut syntax which does both steps in one line. Here the instance is called connection and the function is anonymous.

var connection = new function () {
Quote

<input type = "button" onclick = "connection.openconnetion()" value = "openwebsocket"> 

This is close but:

1. You don't have a variable called connection any more because you deleted it and replaced it with: 

function myfunction()

2. Everything to do with javascript is case sensitive so openconnetion() would need to be openConnection(). 

Paul

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.