NoeJs socket.io module--Implementing Websocket protocol

Keywords: socket Attribute Javascript npm

NoeJs socket. IO module - Implementing Websocket protocol

Believe that the little friends who read this article have a certain understanding of the Websocket protocol, here the little brother will not talk nonsense, directly on the code.

Server side

Background is node, using socket.io module, first install it.

npm install socket.io --save

We create a new js file named socket.js with the content of

// Establish SocketUtil Modular
class SocketUtil {
    constructor(server){
         // Receive well defined server
        this.server = server;
    }
    connect(){
         // Introduce socket.io Modular,And create io
        var io = require('socket.io').listen(this.server);
        io.on('connection', (socket) => {
            console.log(123);
            // Send information to client
            // socket.emit('nicaiback',{msg:'server'});
            // Receive client information and return
            socket.on('nicai', (data) => {
                // Client data
                console.log(data);
                let index = 0;
                var timer = setInterval(() => {
                    if(index >= 10){
                        clearInterval(timer);
                    }
                    var msg = index < 10 ? 'socket Successful connection,Send you a message in a second, no bother.,Ha ha ha' : 'socket Successful connection,I'm not bothering you.,Bye-bye';
                    // Send information to client
                    socket.emit('result', {msg: msg});
                    index++;
                }, 1000);
            });
        });
    }
}

module.exports = SocketUtil;

Then we introduce this module in app.js and add the following code:

// Introduce socket.js
const SocketUtil = require('./public/utils/socket');

// Create service,Be used for socket Connect
var server = http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Hello socket!');
    res.end();
}).listen(1027, '127.0.0.1');

// callsocket.jsMethod in
const Socket = new SocketUtil(server);
Socket.connect();

Client

The client side is much simpler, socket.io,index.html are introduced as follows

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<h1>socket test</h1>
<button onclick="testSocket2()">test socket</button>
</body>
<script src="https://cdn.bootcss.com/socket.io/2.1.1/socket.io.js"></script>
<script>
    // Establish connection
    var socket = io('http://127.0.0.1:1027');

    function testSocket() {
        // Send information to server
        socket.emit('nicai', {msg: 1234});
        // Receiving information from the server
        socket.on('result', function (er) {
            console.log(er);
        });
    }
</script>
</html>

So far, the simplest socket connection has been successful, if it is helpful to the small partners, praise it, thank you!

This article is purely hand-beaten. Please leave a message if there is something wrong with this article! __________ I hope I can help my little friends. Thank you.

Posted by charmedp3 on Tue, 05 Feb 2019 06:06:16 -0800