Nodejs có hỗ trợ WebSockets không?

WebSockets là một công cụ để giao tiếp hai chiều giữa máy khách trình duyệt và máy chủ. Đặc biệt, WebSockets cho phép máy chủ đẩy dữ liệu đến máy khách. Điều này khác với yêu cầu HTTP tiêu chuẩn của bạn bằng cách sử dụng fetch() hoặc Axios vì máy chủ không thể giao tiếp với máy khách trừ khi máy khách gửi yêu cầu trước

WebSockets linh hoạt hơn nhưng cũng khó triển khai và mở rộng quy mô hơn. WebSockets đặt thêm gánh nặng cho nhà phát triển, vì vậy hãy sử dụng chúng một cách tiết kiệm và chỉ khi bạn thực sự cần chúng. Trong bài viết này, bạn sẽ học cách xây dựng một ứng dụng trò chuyện thời gian thực đơn giản bằng WebSockets

Máy chủ WebSocket

Gói ws npm là thư viện WebSocket trên thực tế cho Node. js. Bạn cũng có thể sử dụng Ổ cắm. IO, nhưng ổ cắm. IO là một khung cấp cao hơn trên WebSockets chứ không phải là triển khai giao thức WebSocket

Dưới đây là một ví dụ cơ bản về máy chủ WebSocket theo dõi tất cả các ổ cắm đang mở và gửi tin nhắn gửi đến tất cả các ổ cắm đang mở. Bạn có thể coi đây là một máy chủ trò chuyện đơn giản. khi một người gửi tin nhắn, máy chủ sẽ phát tin nhắn cho mọi người nghe

Máy khách WebSocket trong Nút. js

Kết nối WebSocket có hai thành phần, máy khách và máy chủ. Trong ví dụ trên, bạn đã tạo một máy chủ. Máy khách bắt đầu yêu cầu mở kết nối WebSocket và máy chủ phản hồi các yêu cầu gửi đến để mở kết nối WebSocket

Bạn cũng có thể tạo ứng dụng khách WebSocket trong Node. js sử dụng ws. Điều này rất tốt để kiểm tra logic WebSocket của bạn, mặc dù bạn cũng có thể sử dụng WebSockets để liên lạc giữa các dịch vụ phụ trợ. Dưới đây là một ví dụ về máy khách WebSocket nói chuyện với máy chủ ở trên

Máy khách WebSocket trong Trình duyệt

Nói cách khác, bạn có thể sử dụng lớp WebSocket trong trình duyệt mà không cần có ws hoặc bộ chuyển mã, trừ khi bạn muốn hỗ trợ Internet Explorer 9 hoặc Opera Mini. Dưới đây là một ảnh chụp màn hình từ

Nodejs có hỗ trợ WebSockets không?

Dưới đây là một ví dụ về trang trò chuyện kết nối với máy chủ trên


  
    
  
  
    Chat
    

Send

Lưu ý rằng WebSockets trong trình duyệt có cú pháp hơi khác cho và. Thay vì on('message', messageHandler), bạn nên viết onmessage = messageHandler

Ổ cắm web trong nút. js

Cải thiện bài viết

Lưu bài viết

Thích bài viết

  • Độ khó. Trung bình
  • Cập nhật lần cuối. 21 tháng 2 năm 2022

  • Đọc
  • Bàn luận
  • khóa học
  • Luyện tập
  • Băng hình
  • Cải thiện bài viết

    Lưu bài viết

    Ổ cắm web là gì?
    Web Socket là một giao thức cung cấp giao tiếp full-duplex (multiway) i. e cho phép giao tiếp đồng thời theo cả hai hướng. Đây là một công nghệ web hiện đại, trong đó có một kết nối liên tục giữa trình duyệt của người dùng (máy khách) và máy chủ. Trong kiểu giao tiếp này, giữa máy chủ web và trình duyệt web, cả hai đều có thể gửi tin nhắn cho nhau bất cứ lúc nào. Theo truyền thống trên web, chúng tôi có định dạng yêu cầu/phản hồi trong đó người dùng gửi yêu cầu HTTP và máy chủ phản hồi yêu cầu đó. Điều này vẫn có thể áp dụng trong hầu hết các trường hợp, đặc biệt là những trường hợp sử dụng API RESTful. Nhưng máy chủ cũng cần phải giao tiếp với máy khách mà không bị khách hàng thăm dò (hoặc yêu cầu). Bản thân máy chủ sẽ có thể gửi thông tin đến máy khách hoặc trình duyệt. Đây là nơi Web Socket xuất hiện
    Để sử dụng được Socket trong NodeJS, đầu tiên chúng ta cần cài đặt một phụ thuộc là socket. io. Chúng tôi có thể chỉ cần cài đặt nó bằng cách chạy lệnh bên dưới trong cmd và sau đó thêm phần phụ thuộc này vào tệp javascript phía máy chủ của bạn, đồng thời cài đặt một mô-đun cấp tốc về cơ bản cần thiết cho ứng dụng phía máy chủ
     

     npm install socket.io --save
     npm install express --save

    Ghi chú. npm trong các lệnh trên là viết tắt của trình quản lý gói nút, một nơi mà chúng tôi cài đặt tất cả các phụ thuộc. –cờ lưu không còn cần thiết sau Nút 5. 0. 0, vì tất cả các mô-đun mà chúng tôi hiện cài đặt sẽ được thêm vào phần phụ thuộc.  
    Tạo máy chủ trong tệp JavaScript phía máy chủ của bạn.  

    jav




    const express = require('express');// using express

    const socketIO = require('socket.io');

    const http = require(

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    0_______2_______1

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    2
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    3

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    4

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    5

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    6

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    7

    Bây giờ chúng ta cần tạo kết nối từ phía máy chủ đến phía máy khách thông qua đó máy chủ sẽ có thể gửi dữ liệu đến máy khách.  
     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });

    Tương tự, từ phía máy khách, chúng ta cần thêm tệp script rồi tạo kết nối đến máy chủ thông qua đó người dùng gửi dữ liệu đến máy chủ.  
     

    jav




    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    8
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    9const express = require(0

    const express = require(1

    const express = require(2 const express = require(3

    const express = require(4

    const express = require(5const express = require(6const express = require(7const express = require(8const express = require(9

    'express'0'express'1'express'2

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    1

     

    'express'4

    'express'5

    Bây giờ để gửi tin nhắn hoặc dữ liệu từ máy chủ đến người dùng, chúng tôi tạo ổ cắm “socket. on()” bên trong kết nối mà chúng tôi đã tạo từ phía máy chủ
     

    jav




    'express'6

    const express = require(5'express'8'express'9

    );0'express'1);2);3

    'express'0);5

    Giờ đây, dữ liệu có thể được gửi từ bất kỳ phía nào để kết nối được tạo giữa máy chủ và máy khách. Sau đó, nếu máy chủ gửi tin nhắn thì máy khách có thể nghe tin nhắn đó hoặc nếu máy khách gửi tin nhắn thì máy chủ có thể nghe tin nhắn đó. Vì vậy, chúng tôi phải tạo một ổ cắm cho cả tin nhắn được phát ra và tin nhắn nghe trên cả máy chủ và phía máy khách
    Mã phía máy chủ Ví dụ
     

    jav




    );6'express');

    );9'socket.io');

    // using express2

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    0
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    1

    // using express5

    const express = require(2 // using express7

    // using express8

    const express = require(2 const socketIO = require(0

     

    const socketIO = require(1

    const socketIO = require(2const socketIO = require(3const socketIO = require(4

    'express'0'express'1const socketIO = require(7);

    const socketIO = require(9'socket.io'0

    const socketIO = require(9'socket.io'2);2'socket.io'4

    'socket.io'5'socket.io'6'socket.io'7const express = require(7

    'socket.io'5);0);1const express = require(7

    'socket.io'5);4

    const socketIO = require(9'express'4

     

    'express'0);8

    'express'0const express = require(5'express'8'express'9

    );0'express'1);2);3

    'express'0'express'4

     

    'express'0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    00

    'express'0const express = require(5

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    03
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    04

    );0'express'1

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    07);

    'express'0'express'4

    'express'4

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    7

    đầu ra

    Nodejs có hỗ trợ WebSockets không?

    Mã phía máy khách Ví dụ.  
     

    jav




    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    13

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    14
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    15
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    16
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    17_______2_______18

    'express'0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    20

    );0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    22
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    23
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    18

     

    );0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    26

    'express'0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    28

    'express'0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    30
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    31
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    18

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    33
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    34
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    35
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    18

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    37_______2_______38
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    39
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    40
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    41
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    42
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    43

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    44
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    45
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    46
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    47

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    37_______2_______49

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    33
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    51

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    8
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    9const express = require(0

    const express = require(1

    const express = require(2 const express = require(3

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    58

    const express = require(5const express = require(6const express = require(7const express = require(8const express = require(9

    'express'0'express'1'express'2

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    1

     

    'express'4

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    69

    const express = require(5);2const express = require(7const express = require(8

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    74

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    75
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    76

    'express'4

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    78

    'socket.io'2'express'8'socket.io'4

    'express'0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    83
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    84const express = require(7

    'express'0);0

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    88

    'express'4

     

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    90

    const express = require(5

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    03const express = require(7const express = require(8const express = require(9

    'express'0'express'1

    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    98
    // make a connection with the user from server side
    io.on('connection', (socket)=>{
      console.log('New user connected');
    });
    1

    'express'4

    'express'5

    'express'0const express = require(03

    const express = require(04

    đầu ra.  

    Nodejs có hỗ trợ WebSockets không?


    Ghi chú cá nhân của tôi arrow_drop_up

    Tiết kiệm

    Vui lòng Đăng nhập để nhận xét.

    Nút js có hỗ trợ WebSocket không?

    js thực sự hỗ trợ WebSockets ở cả phía máy chủ và máy khách. Tiếp theo, chúng tôi sẽ yêu cầu bạn tạo một máy chủ WebSocket trong Node. js sẽ chấp nhận kết nối và cung cấp dữ liệu cho khách hàng. Cuối cùng, chúng tôi sẽ triển khai ứng dụng khách WebSocket trong Node

    Làm cách nào để triển khai WebSocket trong Node js?

    Tất cả những gì chúng ta cần làm là gọi Đối tượng WebSocket bằng URI làm tham số . const webSocket = new WebSocket('ws. //máy chủ cục bộ. 443/'); . Vậy là xong, bây giờ chúng ta có thể nhận dữ liệu từ máy chủ WebSocket.

    Nodejs có thể xử lý bao nhiêu WebSocket?

    Giới hạn lý thuyết là 65 nghìn kết nối trên mỗi địa chỉ IP nhưng giới hạn thực tế thường giống hơn 20 nghìn, vì vậy chúng tôi sử dụng nhiều địa chỉ để kết nối 20 nghìn .

    WebSockets trong nút là gì?

    API WebSocket là công nghệ tiên tiến cho phép mở phiên giao tiếp tương tác hai chiều giữa trình duyệt của người dùng và máy chủ . Với API này, bạn có thể gửi tin nhắn đến máy chủ và nhận phản hồi theo sự kiện mà không cần phải thăm dò ý kiến ​​​​của máy chủ để trả lời.