socket session

socket business logic

 

Server logic
1, the introduction of net module creates a server, as a persistent connection net module, when http module non-persistent connection
2, to create a new server, the establishment of a port number
3, with the connection method to link users come before after defining a first array for receiving a user id, user-friendly set of users disconnected stop sending information to the user to add the user, the method Push
message 4, which is used client.on method for receiving data transmitted over a user, with map loop through the array where the user, if present, will pass the message
5, when the user disconnects, the user id Clear


The client logic
1, the introduction of a net module
2, can be introduced into the readline stream file read and write read and write files in the terminal
3, create server
4, through readline. createface create a file input output
5, server client connection via connect, there are three parameters is a port number, ip address, and callback will tune away for receiving the message transmitted over the server
6, using the line method receiving a file, sending a message by client.write

 

 

Server

// server server.js 
const the require net = ( "net") // so use persistent connections net module http non-persistent connection module 
const = net.createServer Server (); 
server.listen (9000); 

const = Clients [] 
// when the user connects to the server to do 
server.on ( "connection", (client) => { 
    // all clients stored in an array, the message will be received when the message transmitted to all users in the past 
    client.id = client.length; 
    clients.push (client); 
    the console.log ( "someone came connection"); 
    
    // client receiving the passed message 
    client.on ( "data", ( Data) => { 
        //console.log(data+ "") 
        // then sends this message to all users 
        clients.map ((Item) => { 
            IF (Item) { 
                item.write (Data); 
            } 
        } ) 
    })
    // When the client disconnects, he will be removed from the array 
    client.on ( "Close", () => { 
         Clients [client.id] = null 
    }) 
   
})

  Client

// client Client.js 
const NET = the require ( "NET"); 
const = the require the readline ( "the readline"); // create a file stream may be read on the terminal writing the file 
const client = new net.Socket ( ); 

const = rl is an readline.createInterface ({ 
    INPUT: process.stdin, 
    Output: proess.stdout 
}) 
// converts the character buffer for the text file 
client.setEncoding ( "UTF8") 

// client connection to the server using the connect method parameters 1: 2 IP address of the port number parameter of 
the client.connect (9000, "127.0.0.1", () => { 
    //client.write("123 "); 
    // server receiving the passed message 
    client.on (" Data ", (Data) => { 
        the console.log (Data) 
    }) 
}); 
// we read and write files on the terminal 
rl.on (" line ", (text ) =>{ 
    // send message 
    client.write (text); 
})

  

Guess you like

Origin www.cnblogs.com/zcccz/p/11280671.html