Ổ cắm. IO là một thư viện cho phép giao tiếp theo thời gian thực, hai chiều và dựa trên sự kiện giữa trình duyệt và máy chủ. Nó bao gồm
- từ nút. máy chủ js. Nguồn. API
- thư viện máy khách Javascript cho trình duyệt [cũng có thể chạy từ Node. js]. Nguồn. API
Ngoài ra còn có một số triển khai ứng dụng khách bằng các ngôn ngữ khác, được duy trì bởi cộng đồng
- Java. https. //github. com/socketio/socket. io-client-java
- C++. https. //github. com/socketio/socket. io-client-cpp
- Nhanh. https. //github. com/socketio/socket. io-client-swift
- phi tiêu. https. //github. com/rikulo/ổ cắm. io-client-phi tiêu
- con trăn. https. //github. com/miguelgrinberg/python-socket
- Mạng lưới. https. //github. com/Quobject/SocketIoClientDotNet
Nó hoạt động như thế nào?
Máy khách sẽ cố gắng thiết lập kết nối WebSocket nếu có thể và sẽ quay lại HTTP long polling nếu không
WebSocket là một giao thức truyền thông cung cấp kênh song công hoàn toàn và độ trễ thấp giữa máy chủ và trình duyệt. Tìm thêm thông tin tại đây
Vì vậy, trong trường hợp tốt nhất, với điều kiện là
- trình duyệt hỗ trợ WebSocket [của tất cả các trình duyệt vào năm 2020]
- không có phần tử nào [proxy, tường lửa,. ] ngăn kết nối WebSocket giữa máy khách và máy chủ
bạn có thể xem xét Ổ cắm. Ứng dụng khách IO dưới dạng trình bao bọc "nhẹ" xung quanh API WebSocket. thay vì viết
const socket = new WebSocket['ws://localhost:3000'];
socket.onopen[[] => {
socket.send['Hello!'];
}];
socket.onmessage[data => {
console.log[data];
}];
Bản saoBạn sẽ có, về phía khách hàng
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
Bản saoAPI ở phía máy chủ cũng tương tự, bạn cũng nhận được một đối tượng
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
2 mở rộng Nút. lớp jsconst io = require['socket.io'][3000];
io.on['connection', socket => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['greetings', 'Hey!', { 'ms': 'jane' }, Buffer.from[[4, 3, 3, 1]]];
// handle the event sent with socket.send[]
socket.on['message', [data] => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['salutations', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
}];
Bản saoỔ cắm. IO cung cấp các tính năng bổ sung trên một đối tượng WebSocket đơn giản, được liệt kê
Nhưng trước tiên, hãy đi vào chi tiết Socket là gì. Thư viện IO không phải là
ổ cắm gì. IO không phải là
Ổ cắm. IO KHÔNG phải là triển khai WebSocket. ổ cắm dù. IO thực sự sử dụng WebSocket làm phương tiện vận chuyển khi có thể, nó thêm siêu dữ liệu bổ sung vào mỗi gói. Đó là lý do tại sao máy khách WebSocket sẽ không thể kết nối thành công với Ổ cắm. Máy chủ IO và Ổ cắm. Máy khách IO sẽ không thể kết nối với máy chủ WebSocket đơn giản
// WARNING: the client will NOT be able to connect!
const socket = io['ws://echo.websocket.org'];
Bản saoNếu bạn đang tìm kiếm một máy chủ WebSocket đơn giản, vui lòng xem qua ws hoặc uWebSockets. js
Ngoài ra còn có các cuộc đàm phán để bao gồm một máy chủ WebSocket trong Node. lõi js
Về phía máy khách, bạn có thể quan tâm đến gói robust-websocket
Ví dụ làm việc tối thiểu
Nếu bạn chưa quen với Node. js, vui lòng xem hướng dẫn Bắt đầu, hướng dẫn lý tưởng cho người mới bắt đầu
Còn không, hãy bắt đầu ngay. Thư viện máy chủ có thể được cài đặt từ NPM
$ npm install socket.io
Bản saoThông tin thêm về cài đặt có thể được tìm thấy trong trang Cài đặt máy chủ
Sau đó, hãy tạo một tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
3, với nội dung sauconst content = require['fs'].readFileSync[__dirname + '/index.html', 'utf8'];
const httpServer = require['http'].createServer[[req, res] => {
// serve the index.html file
res.setHeader['Content-Type', 'text/html'];
res.setHeader['Content-Length', Buffer.byteLength[content]];
res.end[content];
}];
const io = require['socket.io'][httpServer];
io.on['connection', socket => {
console.log['connect'];
}];
httpServer.listen[3000, [] => {
console.log['go to //localhost:3000'];
}];
Bản saoỞ đây, một Node cổ điển. js bắt đầu phục vụ tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
4 và Socket. Máy chủ IO được gắn vào nó. Vui lòng xem trang Khởi tạo máy chủ để biết các cách khác nhau để tạo máy chủHãy tạo tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
4 bên cạnh nóconst socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
0 Bản saoCuối cùng, hãy bắt đầu máy chủ của chúng tôi
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
1 Bản saoVà Voila
Đối tượng
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
2 ở cả hai bên mở rộng lớp EventEmitter, vì vậy- gửi một sự kiện được thực hiện với.
7const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}]; - nhận một sự kiện được thực hiện bằng cách đăng ký một người nghe.
8const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
Để gửi một sự kiện từ máy chủ đến máy khách
Hãy cập nhật tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
3 [phía máy chủ]const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
6 Bản saoVà tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
4 [phía máy khách]const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
8 Bản saoThử nghiệm
Để gửi một tin nhắn từ máy khách đến máy chủ
Hãy cập nhật tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
3 [phía máy chủ]const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
0 Bản saoVà tệp
const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
4 [phía máy khách]const socket = io['ws://localhost:3000'];
socket.on['connect', [] => {
// either with send[]
socket.send['Hello!'];
// or with emit[] and custom event names
socket.emit['salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from[[1, 2, 3, 4]]];
}];
// handle the event sent with socket.send[]
socket.on['message', data => {
console.log[data];
}];
// handle the event sent with socket.emit[]
socket.on['greetings', [elem1, elem2, elem3] => {
console.log[elem1, elem2, elem3];
}];
1 Bản saoThử nghiệm
Bây giờ, hãy đi vào chi tiết các tính năng được cung cấp bởi Socket. Tôi
Đặc trưng
Các tính năng chính của nó là
độ tin cậy
Các kết nối được thiết lập ngay cả khi có sự hiện diện của
- proxy và cân bằng tải
- tường lửa cá nhân và phần mềm diệt virus
Với mục đích này, nó dựa vào Engine. IO, lần đầu tiên thiết lập kết nối bỏ phiếu dài, sau đó cố gắng nâng cấp lên các phương tiện vận chuyển tốt hơn đã được "thử nghiệm" ở bên cạnh, như WebSocket. Vui lòng xem phần để biết thêm thông tin
Hỗ trợ tự động kết nối lại
Trừ khi có hướng dẫn khác, máy khách bị ngắt kết nối sẽ cố gắng kết nối lại mãi mãi cho đến khi máy chủ khả dụng trở lại. Vui lòng xem các tùy chọn kết nối lại có sẵn
phát hiện ngắt kết nối
Một cơ chế nhịp tim được thực hiện tại Engine. Cấp IO, cho phép cả máy chủ và máy khách biết khi nào máy kia không phản hồi nữa
Chức năng đó đạt được với bộ hẹn giờ được đặt trên cả máy chủ và máy khách, với các giá trị thời gian chờ [tham số pingInterval và pingTimeout] được chia sẻ trong quá trình bắt tay kết nối. Những bộ tính giờ đó yêu cầu mọi cuộc gọi máy khách tiếp theo phải được chuyển hướng đến cùng một máy chủ, do đó yêu cầu phiên cố định khi sử dụng nhiều nút
hỗ trợ nhị phân
Bất kỳ cấu trúc dữ liệu tuần tự hóa nào cũng có thể được phát ra, bao gồm
- ArrayBuffer và Blob trong trình duyệt
- ArrayBuffer và bộ đệm trong nút. js
hỗ trợ ghép kênh
Để tạo sự phân tách các mối quan tâm trong ứng dụng của bạn [ví dụ: theo mô-đun hoặc dựa trên quyền], Socket. IO cho phép bạn tạo một số Không gian tên, sẽ hoạt động như các kênh liên lạc riêng biệt nhưng sẽ chia sẻ cùng một kết nối cơ bản