• webSocket chapter 1


    WebSocket.OPEN 是 WebSocket 对象的一个属性,表示 WebSocket 连接的状态。其值为 1,表示连接已经打开并且可以进行通信。

    WebSocket 对象的状态有以下几种:

    • WebSocket.CONNECTING:值为 0,表示连接还未开启。
    • WebSocket.OPEN:值为 1,表示连接已经开启并且可以进行通信。
    • WebSocket.CLOSING:值为 2,表示连接正在关闭。
    • WebSocket.CLOSED:值为 3,表示连接已经关闭或者连接无法建立。
    1. html>
    2. <html>
    3. <head>
    4. <title>WebSocket Demotitle>
    5. head>
    6. <body>
    7. <button id="sendButton">Send Messagebutton>
    8. <script>
    9. const socket = new WebSocket('ws://localhost:3000');
    10. // console.log(socket.binaryType, 'socket.binaryType');
    11. // socket.binaryType = 'arraybuffer';
    12. socket.addEventListener('open', () => {
    13. console.log('Connected to server');
    14. });
    15. socket.addEventListener('message', (event) => {
    16. console.log('Message from server: ', event.data);
    17. console.log(event.data+'', 'event.data');
    18. // blob 转换为字符串
    19. // const reader = new FileReader();
    20. // reader.onload = function () {
    21. // console.log('reader.result', reader.result);
    22. // };
    23. // reader.readAsText(event.data);
    24. });
    25. document.getElementById('sendButton').addEventListener('click', () => {
    26. socket.send('Hello Server!');
    27. });
    28. script>
    29. body>
    30. html>

     

    1. /*
    2. * when you set a variable | function | logic | declaration,please add a annotation as possible,God Bless You 🎈
    3. *
    4. * @Author: guoyongkun 👨‍💻
    5. * @Date: 2024-02-24 09:17:47
    6. * @LastEditors: guoyongkun 👨
    7. * @LastEditTime: 2024-02-24 10:50:19
    8. * @FilePath: /websocket/server.js
    9. * @Description: please set some description for this file , let's improve the code maintainability 💡 ...
    10. */
    11. // server.js
    12. const express = require('express');
    13. const WebSocket = require('ws');
    14. const http = require('http');
    15. // const Blob = require('blob');
    16. const app = express();
    17. // const Blob = require('fetch-blob');
    18. // import Blob from 'fetch-blob';
    19. // console.log('app99',app);
    20. const server = http.createServer(app);
    21. const wss = new WebSocket.Server({server});
    22. wss.on('connection', (ws) => {
    23. console.log('Client connected');
    24. ws.on('message', (message) => {
    25. console.log('Received: ' + message);
    26. console.log(message + 'message1');
    27. // Broadcast the message to all clients
    28. console.log(wss.clients.size, 'wss.clients.size');
    29. // console.log(wss.clients,'wss.clients');
    30. wss.clients.forEach((client) => {
    31. if (client.readyState === WebSocket.OPEN) {
    32. console.log('Send message to client', client.readyState);
    33. console.log(message, 'message2');
    34. client.send(message);
    35. }
    36. });
    37. });
    38. ws.on('close', () => {
    39. console.log('Client disconnected');
    40. });
    41. });
    42. server.listen(3000, () => {
    43. console.log('Server is running on port 3000');
    44. });
    45. import('fetch-blob').then((module) => {
    46. // console.log(module, 'module.default');
    47. // let blob1 = new module.Blob(['123'], {type: 'text/plain'});
    48. // console.log(blob1+'1', 'blob1');
    49. });
    50. // 创建一个长度为 10 的 Buffer,填充为 0
    51. const buf1 = Buffer.alloc(10);
    52. console.log(buf1, 'buf1');//
    53. buf1.write('123');
    54. console.log(buf1, 'buf1');//
    55. // 读取 Buffer 中的数据
    56. for (let i = 0; i < buf1.length; i++) {
    57. console.log(buf1[i]); // 打印每个字节 // 49 50 51 0 0 0 0 0 0 0
    58. }
    59. //读取 Buffer 中的数据
    60. console.log(buf1.toString()); /// 123

  • 相关阅读:
    css滤镜
    《统计学习方法》 第十五章 奇异值分解
    [c++基础]-vector类
    【C++】搜索二叉树面试oj题
    Ubuntu16.04 部署 TensorFlow2-GPU版本(虚拟环境)
    玩转ASP.NET Core 6.0框架-序言
    使用CrawlSpider爬取全站数据。
    电商平台API接口的作用到底是什么?重要性又是什么?具体接入方式?
    RS练习 - PTE(一)
    四参数旋转角异常,平面坐标方位角不准确的问题
  • 原文地址:https://blog.csdn.net/gu2022_3_5_21_23/article/details/136270473