Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to open a tcp connection in javascript

//
// This example shows a simple TCP echo server.
// The server will listen on port 6789 and respond back with whatever
// has been sent to the server.
//

 //  Request permission to listen on port 6789
 navigator.tcpServerPermission.requestPermission({"localPort":6789}).then(
   () => {
     // Permission was granted

     //  Create a new server socket that listens on port 6789
     var myServerSocket = new TCPServerSocket({"localPort": 6789});

     // Listen for connection attempts from TCP clients.
     listenForConnections();
     function listenForConnections() {
       myServerSocket.listen().then(
           connectedSocket => {
           // A connection has been accepted

             console.log ("Connection accepted from address: " +
                           connectedSocket.remoteAddress + " port: " +
                           connectedSocket.remotePort);

             var reader = connectedSocket.readable.getReader();

             // Wait for data
             waitForData();
             function waitForData () {
               reader.read().then(
                 ({ value, done }) => {
                   if (done) return;

                   // Data in buffer, read it
                   console.log("Received: " + value);

                   // Send data back
                   connected.writeable.write(value).then(
                     () => console.log("Sending data succeeded"),
                     e => console.error("Failed to send: ", e);
                   });
                   // Continue to wait for data
                   waitForData ();
                 }
               );

             }

             // Continue to listen for new connections
             listenForConnections();
       },
           e => {
             console.error("A client connection attempt failed: ", e);

             // Continue to listen for new connections
             listenForConnections();
           }

       );
     }

     // Log result of TCP server socket creation attempt.
     myServerSocket.opened.then(
       () => {
         console.log("TCP server socket created sucessfully");
       },
       e =>console.error("TCP server socket creation failed due to error: ", e);
     );

     // Handle TCP server closed, either as a result of the webapp
     // calling myServerSocket.close() or due to an error.
     myServerSocket.closed.then(
       () => {
          console.log("TCP server socket has been cleanly closed");
       },
       e => console.error("TCP server socket closed due to error: ", e);
     );

   },
   e => console.error("TCP Server Socket on local port 6789 was denied
                      due to error: ", e);
 );
Comment

PREVIOUS NEXT
Code Example
Javascript :: call a javascript function at a specific time of day 
Javascript ::  
Javascript ::  
Javascript :: react clone element 
::  
:: Install PHP debugbar 
::  
Javascript :: how to add multiple event listener in javascript 
Javascript :: GET FORM VALUE 
::  
Javascript :: javascript Clone Array Using Spread Operator 
::  
Javascript :: merge two binary tree 
:: code to convert rgb to hsl color 
Javascript ::  
Javascript :: angular2-tree-diagram 
::  
:: google script new date 
Javascript :: explode array inside string javascript 
Javascript ::  
::  
Javascript :: what is axios used for 
Javascript :: call local function javascript 
Javascript ::  
Javascript :: dot notation vs bracket notation 
Javascript :: eval javascript 
::  
:: difference between single quotes and double quotes in javascript 
Javascript ::  
Javascript ::  
ADD CONTENT
Topic
Content
Source link
Name
8+3 =