<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          首頁

          【Git】 取消上一次commit或push

          前端達人

          (轉自csdn)

          目錄

          一、取消上一次commit

          方法1:使用 Git reset

          方法2:使用 Git revert

          方法3:使用 Git checkout

          二、取消上一次push

          git push --force

          git reflog

          git reset

          git push --force

          三、擴展

          commit參數

          --mixed

          --soft

          --hard

          --amend


          一、取消上一次commit

          如果你需要取消上一次的 Git 提交,有幾個不同的方法可以實現。其中包括撤消提交、提交到新的分支、使用 Git 回滾等等。

          下面介紹三種方法:

          方法1:使用 Git reset

          使用 Git reset 命令來取消上一次提交

          git reset HEAD~1
          

          這會把 HEAD 指針移回上一個提交(HEAD~1),并清除最后一次提交的內容。

          git reset HEAD^

          撤回兩次或者n次

          git reset HEAD~2
          

          方法2:使用 Git revert

          使用 Git revert 命令來撤消上一次提交并創建一個新的提交來撤消原來的提交:

          git revert HEAD
          

          這會創建一個新的提交來撤消提交之前的更改。在命令行中輸入該命令后,你需要編輯撤消的提交信息,以便 Git 創建一個新的提交。

          方法3:使用 Git checkout

          還可以使用 Git checkout 命令來將工作樹恢復到上一次提交的狀態。這將清除所有未提交的更改,所以請確保你有一個備份:

          git checkout HEAD~1
          

          這會將工作樹恢復到上一次提交的狀態,也就是你上一次提交之前的狀態。請注意,這里的修改都將被丟棄。

          無論你使用的是哪種方法,請確保在撤消提交之前首先備份你的工作。這可以幫助你避免在操作過程中意外刪除無法恢復的內容。

          二、取消上一次push

          如果你已經push了代碼,并且想要撤回這個commit,可以通過以下步驟實現:

          git push --force

          首先,在使用git push命令時,需要加上--force參數,強制覆蓋遠程倉庫上已經存在的commit。命令如下:

          git push --force origin <branch_name>
          

          其中,<branch_name>表示你要撤銷的分支名稱。

          git reflog

          如果在本地倉庫沒有回到該commit的上一個狀態,需要使用git reflog命令找到該commit的SHA-1值。命令如下:

          git reflog
          

          該命令會列出整個Git倉庫的提交歷史記錄,包括HEAD指針所指向的提交和已經被廢棄的提交。

          git reset

          找到要回到的某個commit的SHA-1值,然后使用如下命令回到該commit的狀態:

          git reset --hard <commit_SHA-1>
          

          其中,<commit_SHA-1>表示要回到的commit的SHA-1值。

          git push --force

          然后使用之前的推送命令進行推送,添加--force參數,覆蓋遠程倉庫的歷史提交記錄。命令如下:

          git push --force origin <branch_name>
          

          提醒:使用git push --force命令可能會導致遠程倉庫、其他成員的倉庫和歷史版本產生不可逆的影響,因此操作時需要謹慎。一般情況下,在工作流中使用git revert命令回滾某個commit,以保證版本控制的完整性和可維護性。

          三、擴展

          git log  查看提交日志

          commit參數

          --mixed

           不刪除工作空間改動代碼,撤銷 commit,并撤銷 git add . 操作

           git reset --mixed HEAD^ 效果等同 git reset HEAD^

          --soft

              不刪除工作空間改動代碼,撤銷commit,不撤銷git add .

          --hard

              刪除工作空間改動代碼,撤銷commit,撤銷git add .

          --amend

          修改注釋, 進入vim編輯器, 改完:wq即可

           

          藍藍設計(www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的大數據可視化界面設計、B端界面設計桌面端界面設計、APP界面設計、圖標定制用戶體驗設計、交互設計UI咨詢、高端網站設計、平面設計,以及相關的軟件開發服務,咨詢電話:01063334945。

          關鍵詞:UI咨詢、UI設計服務公司、軟件界面設計公司、界面設計公司、UI設計公司、UI交互設計公司、數據可視化設計公司用戶體驗公司、高端網站設計公司

          銀行金融軟件UI界面設計能源及監控軟件UI界面設計、氣象行業UI界面設計、軌道交通界面設計、地理信息系統GIS UI界面設計、航天軍工軟件UI界面設計醫療行業軟件UI界面設計、教育行業軟件UI界面設計、企業信息化UI界面設計、軟件qt開發、軟件wpf開發、軟件vue開發

          國內 Linux 集群服務器網絡差無法 Git Clone 大項目的解決辦法

          前端達人

          GitHub 是一個軟件源代碼托管服務平臺,Linux 上使用的大部分工具都可以在上面獲得。

          但是由于 GitHub 的服務器在國外,國內訪問經常出現網絡問題導致訪問失敗,尤其是使用 git clone 對 repo 進行克隆的時候,會經常出現因網絡問題而克隆失敗。

          Linux 集群服務器的用戶,在比較難實現科學上網的前提下,對大型 repo 克隆的任務顯得尤為困難,而且想使用 gradle build 的話,git clone 似乎是唯一選擇。

          既然 git clone 一兩次不成功,那我就想辦法讓他 clone 到成功為止,于是我就寫了一個循環腳本,將它投遞到計算節點上 24h 運行,直到它克隆成功才結束任務。

          以克隆 GATK (repo 大小約 300mb)為例,腳本如下:

          #!/bin/bash
          #SBATCH --job-name=GitClone        #任務名稱
          #SBATCH --nodelist=litchi-0-1    #計算節點名稱
          #SBATCH --partition=WHEEL        #使用 WHEEL 用戶組
          #SBATCH --nodes=1                #使用計算節點數量
          #SBATCH --ntasks=1                #使用 1 個線程
          #SBATCH --mail-type=end            #任務結束時發送郵件
          #SBATCH --mail-user=***@qq.com  #郵箱地址
          #SBATCH --output=/home/$USER/Slurm_JobLogs/JobLog_%j_%x.log        #標準輸出保存路徑
          #SBATCH --error=/home/$USER/Slurm_JobLogs/JobLog_%j_%x.err        #標準錯誤保存路徑
          #------------------------------------------------------------
          # PRINT JOB'S INFORMATION
            source /home/$USER/.bashrc
            threads=$SLURM_NTASKS
            echo "## Job:[$SLURM_JOB_NAME]"
            echo "## Node:[$SLURM_NODELIST]"
            echo "## CPUs:[$SLURM_NTASKS]"
            echo "## Mem:[$SLURM_MEM_PER_NODE]"
            echo `date`
            echo -e $(printf -- "-%.0s" {1..100})"\n" #cut-off-line
          #------------------------------------------------------------
          # PATH
            repo_url="https://github.com/broadinstitute/gatk.git"
            repo_name="gatk"
          #------------------------------------------------------------
          # SHELL
            cd /home/$USER/Accessories/Softwares
            while true; do                #循環執行任務
              git clone $repo_url            #執行 git clone
              if [ $? -eq 0 ]; then        #如果以上命令的退出碼等于 0 (任務成功)
                echo "Git clone successful!"
                break                        #則退出循環
              else                        #如果命令的退出碼不等于 0 (任務失敗)
                echo "Git clone failed:( Retrying..."
                rm -rf ./${repo_name}        #則刪除這個repo
                sleep 1                    #睡眠 1 秒緩沖一下繼續循環
              fi
            done
          #------------------------------------------------------------
          # JOB ENDS
            echo -e $(printf -- "-%.0s" {1..100}) #cut-off-line
            echo "## Runtime:[`sacct -j $SLURM_JOB_ID --format=Elapsed --noheader | awk '{print $1}' | sed -n '2p'`]"
          
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24
          • 25
          • 26
          • 27
          • 28
          • 29
          • 30
          • 31
          • 32
          • 33
          • 34
          • 35
          • 36
          • 37
          • 38
          • 39
          • 40
          • 41
          • 42

          這個腳本的路徑為 ~/Scripts/GitClone_GATK.sh

          將任務投遞到計算節點運行:

          sbatch ~/Scripts/GitClone_GATK.sh
          
          • 1

          最終,這個任務運行了 40 次,歷時 13h,終于是克隆成功了!

          藍藍設計(www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的大數據可視化界面設計B端界面設計、桌面端界面設計APP界面設計、圖標定制、用戶體驗設計交互設計、UI咨詢高端網站設計、平面設計,以及相關的軟件開發服務,咨詢電話:01063334945。

          關鍵詞:UI咨詢、UI設計服務公司、軟件界面設計公司、界面設計公司、UI設計公司、UI交互設計公司、數據可視化設計公司、用戶體驗公司、高端網站設計公司

          銀行金融軟件UI界面設計、能源及監控軟件UI界面設計、氣象行業UI界面設計、軌道交通界面設計、地理信息系統GIS UI界面設計、航天軍工軟件UI界面設計醫療行業軟件UI界面設計教育行業軟件UI界面設計、企業信息化UI界面設計、軟件qt開發軟件wpf開發、軟件vue開發

          GIT生成SSH公鑰圖文教程 轉自csdn

          前端達人

          GIT介紹

          GIT是一種分布式版本控制系統,用于追蹤文件的變化和協作開發。本文將詳細介紹GIT的基本架構、工作流程和常用命令,并對其優勢和應用場景進行分析。

          1. GIT的基本架構

          GIT的基本架構由三個主要組件組成:工作區(Working Directory)、暫存區(Stage)和倉庫(Repository)。

          • 工作區即本地文件夾,用于存放項目文件。
          • 暫存區是位于工作區與倉庫之間的緩沖區域,用于臨時存儲修改。
          • 倉庫則是存放項目歷史記錄的地方,包含項目所有文件的完整歷史。

          2. GIT的工作流程

          GIT的工作流程通常包括以下幾個步驟:

          • 初始化倉庫:使用git init命令初始化一個空白的倉庫。
          • 添加文件:使用git add命令將文件添加到暫存區中。
          • 提交修改:使用git commit命令將暫存區的修改提交到倉庫中。
          • 分支管理:使用git branch命令創建、切換和刪除分支。
          • 遠程倉庫:使用git remote命令管理遠程倉庫,并使用git pushgit pull命令與遠程倉庫進行數據同步。

          3. GIT常用命令

          以下是GIT的一些常用命令及其功能:

          • git init:初始化一個新的倉庫。
          • git add <file>:將指定文件添加到暫存區中。
          • git commit -m "<message>":提交暫存區的修改并添加注釋。
          • git status:顯示工作區和暫存區的狀態。
          • git log:顯示倉庫的提交歷史。
          • git branch:管理分支,包括創建、切換和刪除分支。
          • git checkout [branch]:切換到指定分支。
          • git merge <branch>:將指定分支合并到當前分支。
          • git remote add <name> <url>:添加遠程倉庫。
          • git push <remote> <branch>:將本地分支推送到遠程倉庫。
          • git pull <remote> <branch>:從遠程倉庫拉取最新代碼。

          4. GIT的優勢

          GIT具有以下幾個優勢:

          • 分布式:每個開發者都擁有完整的倉庫副本,可以在離線環境下進行工作,并能方便地處理分支操作和合并沖突。
          • 高效性:GIT采用了快照方式保存文件,不會重復存儲相同的內容,大幅節省存儲空間。
          • 數據完整性:使用哈希算法確保每個文件和每次提交都有唯一的標識,可以有效避免數據損壞和篡改。
          • 可擴展性:GIT具有良好的插件和擴展支持,可以根據需求靈活地進行功能擴展。

          5. GIT的應用場景

          由于GIT的優勢和高效性,它廣泛應用于軟件開發中的版本控制和協作管理。以下是幾個常見的應用場景:

          • 代碼版本控制:開發人員可以使用GIT來追蹤和管理代碼的版本變化,輕松地回退、回顧和比較代碼的不同版本。
          • 多人協作開發:GIT支持跨團隊和跨地域的多人協作開發,能夠有效解決代碼沖突,并提供完整的歷史記錄和審計功能。
          • 敏捷開發:GIT的分支管理和快速迭代特性非常適合敏捷開發方法,可以幫助團隊快速迭代并保持項目的整潔和穩定。

          綜上所述,GIT作為一種強大的分布式版本控制系統,在軟件開發中發揮著重要的作用。通過了解GIT的基本架構、工作流程和常用命令,開發人員可以更好地利用和實踐GIT,提高代碼管理和協作效率。

          教程

          1.下載安裝git客戶端

          此處省略1萬字,本人是通過idea開發工具下載的git.

          2.鼠標右鍵進入,給 GIT base Here 

           

          輸入 生成SSH公鑰命令

          ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

          然后需要輸入的地方,回車,如圖所示,生成ssh公鑰成功

           

          在找到公鑰生成的位置,打開id_rsa.pub文件,即可找到ssh公鑰

           

           在git服務平臺如碼云、conding、github等里添加自己公鑰

           

          補充知識

          SSH公鑰是一種加密技術,用于實現安全的遠程登錄和文件傳輸。它使用非對稱加密算法,將用戶的公鑰保存在被訪問的服務器上,而私鑰則由用戶自己保管。

          當用戶要進行遠程登錄或文件傳輸時,客戶端會生成一對公鑰和私鑰,并將公鑰發送給服務器。服務器將收到的公鑰存儲起來。當用戶進行身份驗證時,服務器會向客戶端發送一個隨機的挑戰,并使用存儲的公鑰對其進行加密。用戶收到挑戰后,使用自己保管的私鑰進行解密并返回結果給服務器。如果解密結果正確,身份驗證就成功了。

          SSH公鑰具有以下優點:

          1. 安全性:使用非對稱加密算法,確保通信和數據傳輸的安全性。
          2. 方便性:無需記住密碼,只需要保管好私鑰即可。
          3. 靈活性:可以在多個終端之間共享公鑰,方便用戶同時訪問多臺服務器。
          4. 可審計性:公鑰在服務器上留下痕跡,方便審計和管理。

          為了確保SSH公鑰的安全,用戶應該妥善保管自己的私鑰,并定期更新公鑰,防止被惡意利用。此外,服務器管理員也應確保正確配置和管理公鑰,避免安全漏洞的出現。

          git入門:Linux操作系統下 git環境搭建 | 生成公私密鑰

          前端達人

          前言

          該示例是在ubuntu下完成的

           

           

          一、安裝 git 和 ssh

          sudo apt-get install git
          sudo apt-get install openssh-server

          二、配置公鑰

          因為git和 GitHub之間是通過ssh加密傳輸的,因此需要配置公鑰,所以需要先生成公私密鑰。

          命令:該郵箱是你注冊GitHub的郵箱地址

          ssh-keygen -t rsa -C “xxxxxxxxxx@163.com”

          如圖,表示生成成功

          三、得到公鑰在GitHub配置

          進入 .ssh 文件里,查看id_rsa.pub,表示公鑰(public)

          復制該密鑰到GitHub里配置

          標題title隨便起

          四、查看能否通信成功

          輸入該命令:如果出現下面這句話,就說明成功了。

          ssh -T git@github.com

          五、配置郵箱和用戶名

          通過以下兩個命令配置用戶名和郵箱,以后提交代碼文件,就會附帶該用戶的信息。

          git config --global user.name " xxx "
          git config --global user.email " xxx@xx.com"

          在 .gitconfig文件可查看

          藍藍設計(www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的大數據可視化界面設計、B端界面設計、桌面端界面設計APP界面設計、圖標定制用戶體驗設計、交互設計、UI咨詢高端網站設計、平面設計,以及相關的軟件開發服務,咨詢電話:01063334945。

          關鍵詞:UI咨詢、UI設計服務公司、軟件界面設計公司、界面設計公司、UI設計公司、UI交互設計公司、數據可視化設計公司、用戶體驗公司高端網站設計公司

          銀行金融軟件UI界面設計、能源及監控軟件UI界面設計、氣象行業UI界面設計軌道交通界面設計、地理信息系統GIS UI界面設計航天軍工軟件UI界面設計、醫療行業軟件UI界面設計、教育行業軟件UI界面設計、企業信息化UI界面設計、軟件qt開發、軟件wpf開發、軟件vue開發

          4個步驟,總結B端設計師的競品分析方法|北京藍藍UI設計公司

          周周

          競品分析方法很多,但大多數是偏向 B 端產品經理方面的或者是 C 端設計師的,真正在 B 端設計中的方法比較少,這次為大家介紹適合 B 端設計師使用的競品分析方法。

          如何拆解UI界面設計?試試這個三維分析法?。本┧{藍UI設計公司

          周周

          界面設計就是將一堆分散的元素、內容或者組件,通過正確使用可視層次結構,進行合理的規劃和重組,有目的地呈現在用戶的面前,這樣有助于內容傳達和提升用戶使用效率,幫助用戶快速解決問題。

          10個產品細節剖析,看看高手是如何做設計的?。本┧{藍UI設計公司

          周周

          俗話說:“細節決定成敗”,對于設計而言亦是如此,把設計做到極致才能在競爭中脫穎而出。對于產品設計師來說不僅需要有發現的眼睛,也要有做到極致的習慣。通過優秀的設計案例不斷培養自己的設計思維,才能在項目中不斷創新和做到極致。

          最近黑馬哥發現了一些優秀的設計細節,相信這些小小的細節必然可以帶來更好的體驗,助力產品帶給用戶更好的使用體驗。

           

          4000字干貨!幫你快速了解產品設計中的心智模式|北京藍藍UI設計公司

          周周

          進入主題之前,向大家強烈推薦李睿秋先生的《打開心智》一書,雖然書名有一點成功學,但是內容非常有參考性。本次分享內容的核心也來源于《打開心智》一書,自己僅做了內容的整理與 PPT 設計。

          好看的圣誕樹(動態效果) 轉自 csdn

          前端達人

          、制作方法 

          1.復制代碼到Dreamweaver或HBuilder或vscode中

          2.點擊運行---運行到瀏覽器---選擇你要打開的瀏覽器

          3.打開后會出現這個界面,前四個是固定音樂,最后一個是自主選擇的音樂,你可以選擇你電腦上的歌曲,什么歌曲都行(第一次打開可能會有點慢,稍等片刻即可,選擇音樂的時候點一下沒反應的話多點幾下即可,第一次打開這屬于正常現象)

           

          4.特別提醒:打開的時候電腦一定要處于聯網狀態
           

          三、源代碼

           
          1.  
            <!DOCTYPE html>
          2.  
            <html lang="en">
          3.  
             
          4.  
            <head>
          5.  
            <meta charset="UTF-8">
          6.  
             
          7.  
            <title>圣誕樹</title>
          8.  
             
          9.  
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
          10.  
             
          11.  
            <style>
          12.  
            * {
          13.  
            box-sizing: border-box;
          14.  
            }
          15.  
             
          16.  
             
          17.  
            body {
          18.  
            margin: 0;
          19.  
            height: 100vh;
          20.  
            overflow: hidden;
          21.  
            display: flex;
          22.  
            align-items: center;
          23.  
            justify-content: center;
          24.  
            background: #161616;
          25.  
            color: #c5a880;
          26.  
            font-family: sans-serif;
          27.  
            }
          28.  
             
          29.  
             
          30.  
            label {
          31.  
            display: inline-block;
          32.  
            background-color: #161616;
          33.  
            padding: 16px;
          34.  
            border-radius: 0.3rem;
          35.  
            cursor: pointer;
          36.  
            margin-top: 1rem;
          37.  
            width: 300px;
          38.  
            border-radius: 10px;
          39.  
            border: 1px solid #c5a880;
          40.  
            text-align: center;
          41.  
            }
          42.  
             
          43.  
             
          44.  
            ul {
          45.  
            list-style-type: none;
          46.  
            padding: 0;
          47.  
            margin: 0;
          48.  
            }
          49.  
             
          50.  
             
          51.  
            .btn {
          52.  
            background-color: #161616;
          53.  
            border-radius: 10px;
          54.  
            color: #c5a880;
          55.  
            border: 1px solid #c5a880;
          56.  
            padding: 16px;
          57.  
            width: 300px;
          58.  
            margin-bottom: 16px;
          59.  
            line-height: 1.5;
          60.  
            cursor: pointer;
          61.  
            }
          62.  
             
          63.  
            .separator {
          64.  
            font-weight: bold;
          65.  
            text-align: center;
          66.  
            width: 300px;
          67.  
            margin: 16px 0px;
          68.  
            color: #a07676;
          69.  
            }
          70.  
             
          71.  
             
          72.  
            .title {
          73.  
            color: #a07676;
          74.  
            font-weight: bold;
          75.  
            font-size: 1.25rem;
          76.  
            margin-bottom: 16px;
          77.  
            }
          78.  
             
          79.  
             
          80.  
            .text-loading {
          81.  
            font-size: 2rem;
          82.  
            }
          83.  
            </style>
          84.  
             
          85.  
            <script>
          86.  
            window.console = window.console || function (t) { };
          87.  
            </script>
          88.  
             
          89.  
             
          90.  
             
          91.  
            <script>
          92.  
            if (document.location.search.match(/type=embed/gi)) {
          93.  
            window.parent.postMessage("resize", "*");
          94.  
            }
          95.  
            </script>
          96.  
             
          97.  
             
          98.  
            </head>
          99.  
             
          100.  
            <body translate="no">
          101.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/build/three.min.js"></script>
          102.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/EffectComposer.js"></script>
          103.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/RenderPass.js"></script>
          104.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/ShaderPass.js"></script>
          105.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/shaders/CopyShader.js"></script>
          106.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/shaders/LuminosityHighPassShader.js"></script>
          107.  
            <script src="https://cdn.jsdelivr.net/npm/three@0.115.0/examples/js/postprocessing/UnrealBloomPass.js"></script>
          108.  
             
          109.  
            <div id="overlay">
          110.  
            <ul>
          111.  
            <li class="title">請選擇音樂</li>
          112.  
            <li>
          113.  
            <button class="btn" id="btnA" type="button">
          114.  
            Snowflakes Falling Down by Simon Panrucker
          115.  
            </button>
          116.  
            </li>
          117.  
            <li><button class="btn" id="btnB" type="button">This Christmas by Dott</button></li>
          118.  
            <li><button class="btn" id="btnC" type="button">No room at the inn by TRG Banks</button></li>
          119.  
            <li><button class="btn" id="btnD" type="button">Jingle Bell Swing by Mark Smeby</button></li>
          120.  
            <li class="separator">或者</li>
          121.  
            <li>
          122.  
            <input type="file" id="upload" hidden />
          123.  
            <label for="upload">Upload File</label>
          124.  
            </li>
          125.  
            </ul>
          126.  
            </div>
          127.  
             
          128.  
            <script id="rendered-js">
          129.  
            const { PI, sin, cos } = Math;
          130.  
            const TAU = 2 * PI;
          131.  
             
          132.  
            const map = (value, sMin, sMax, dMin, dMax) => {
          133.  
            return dMin + (value - sMin) / (sMax - sMin) * (dMax - dMin);
          134.  
            };
          135.  
             
          136.  
            const range = (n, m = 0) =>
          137.  
            Array(n).
          138.  
            fill(m).
          139.  
            map((i, j) => i + j);
          140.  
             
          141.  
            const rand = (max, min = 0) => min + Math.random() * (max - min);
          142.  
            const randInt = (max, min = 0) => Math.floor(min + Math.random() * (max - min));
          143.  
            const randChoise = arr => arr[randInt(arr.length)];
          144.  
            const polar = (ang, r = 1) => [r * cos(ang), r * sin(ang)];
          145.  
             
          146.  
            let scene, camera, renderer, analyser;
          147.  
            let step = 0;
          148.  
            const uniforms = {
          149.  
            time: { type: "f", value: 0.0 },
          150.  
            step: { type: "f", value: 0.0 }
          151.  
            };
          152.  
             
          153.  
            const params = {
          154.  
            exposure: 1,
          155.  
            bloomStrength: 0.9,
          156.  
            bloomThreshold: 0,
          157.  
            bloomRadius: 0.5
          158.  
            };
          159.  
             
          160.  
            let composer;
          161.  
             
          162.  
            const fftSize = 2048;
          163.  
            const totalPoints = 4000;
          164.  
             
          165.  
            const listener = new THREE.AudioListener();
          166.  
             
          167.  
            const audio = new THREE.Audio(listener);
          168.  
             
          169.  
            document.querySelector("input").addEventListener("change", uploadAudio, false);
          170.  
             
          171.  
            const buttons = document.querySelectorAll(".btn");
          172.  
            buttons.forEach((button, index) =>
          173.  
            button.addEventListener("click", () => loadAudio(index)));
          174.  
             
          175.  
             
          176.  
            function init() {
          177.  
            const overlay = document.getElementById("overlay");
          178.  
            overlay.remove();
          179.  
             
          180.  
            scene = new THREE.Scene();
          181.  
            renderer = new THREE.WebGLRenderer({ antialias: true });
          182.  
            renderer.setPixelRatio(window.devicePixelRatio);
          183.  
            renderer.setSize(window.innerWidth, window.innerHeight);
          184.  
            document.body.appendChild(renderer.domElement);
          185.  
             
          186.  
            camera = new THREE.PerspectiveCamera(
          187.  
            60,
          188.  
            window.innerWidth / window.innerHeight,
          189.  
            1,
          190.  
            1000);
          191.  
             
          192.  
            camera.position.set(-0.09397456774197047, -2.5597086635726947, 24.420789670889008);
          193.  
            camera.rotation.set(0.10443543723052419, -0.003827152981119352, 0.0004011488708739715);
          194.  
             
          195.  
            const format = renderer.capabilities.isWebGL2 ?
          196.  
            THREE.RedFormat :
          197.  
            THREE.LuminanceFormat;
          198.  
             
          199.  
            uniforms.tAudioData = {
          200.  
            value: new THREE.DataTexture(analyser.data, fftSize / 2, 1, format)
          201.  
            };
          202.  
             
          203.  
             
          204.  
            addPlane(scene, uniforms, 3000);
          205.  
            addSnow(scene, uniforms);
          206.  
             
          207.  
            range(10).map(i => {
          208.  
            addTree(scene, uniforms, totalPoints, [20, 0, -20 * i]);
          209.  
            addTree(scene, uniforms, totalPoints, [-20, 0, -20 * i]);
          210.  
            });
          211.  
             
          212.  
            const renderScene = new THREE.RenderPass(scene, camera);
          213.  
             
          214.  
            const bloomPass = new THREE.UnrealBloomPass(
          215.  
            new THREE.Vector2(window.innerWidth, window.innerHeight),
          216.  
            1.5,
          217.  
            0.4,
          218.  
            0.85);
          219.  
             
          220.  
            bloomPass.threshold = params.bloomThreshold;
          221.  
            bloomPass.strength = params.bloomStrength;
          222.  
            bloomPass.radius = params.bloomRadius;
          223.  
             
          224.  
            composer = new THREE.EffectComposer(renderer);
          225.  
            composer.addPass(renderScene);
          226.  
            composer.addPass(bloomPass);
          227.  
             
          228.  
            addListners(camera, renderer, composer);
          229.  
            animate();
          230.  
            }
          231.  
             
          232.  
            function animate(time) {
          233.  
            analyser.getFrequencyData();
          234.  
            uniforms.tAudioData.value.needsUpdate = true;
          235.  
            step = (step + 1) % 1000;
          236.  
            uniforms.time.value = time;
          237.  
            uniforms.step.value = step;
          238.  
            composer.render();
          239.  
            requestAnimationFrame(animate);
          240.  
            }
          241.  
             
          242.  
            function loadAudio(i) {
          243.  
            document.getElementById("overlay").innerHTML =
          244.  
            '<div class="text-loading">正在下載音樂,請稍等...</div>';
          245.  
            const files = [
          246.  
            "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/no_curator/Simon_Panrucker/Happy_Christmas_You_Guys/Simon_Panrucker_-_01_-_Snowflakes_Falling_Down.mp3",
          247.  
            "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/no_curator/Dott/This_Christmas/Dott_-_01_-_This_Christmas.mp3",
          248.  
            "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/TRG_Banks/TRG_Banks_Christmas_Album/TRG_Banks_-_12_-_No_room_at_the_inn.mp3",
          249.  
            "https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Mark_Smeby/En_attendant_Nol/Mark_Smeby_-_07_-_Jingle_Bell_Swing.mp3"];
          250.  
             
          251.  
            const file = files[i];
          252.  
             
          253.  
            const loader = new THREE.AudioLoader();
          254.  
            loader.load(file, function (buffer) {
          255.  
            audio.setBuffer(buffer);
          256.  
            audio.play();
          257.  
            analyser = new THREE.AudioAnalyser(audio, fftSize);
          258.  
            init();
          259.  
            });
          260.  
             
          261.  
             
          262.  
             
          263.  
             
          264.  
            }
          265.  
             
          266.  
             
          267.  
            function uploadAudio(event) {
          268.  
            document.getElementById("overlay").innerHTML =
          269.  
            '<div class="text-loading">請稍等...</div>';
          270.  
            const files = event.target.files;
          271.  
            const reader = new FileReader();
          272.  
             
          273.  
            reader.onload = function (file) {
          274.  
            var arrayBuffer = file.target.result;
          275.  
             
          276.  
            listener.context.decodeAudioData(arrayBuffer, function (audioBuffer) {
          277.  
            audio.setBuffer(audioBuffer);
          278.  
            audio.play();
          279.  
            analyser = new THREE.AudioAnalyser(audio, fftSize);
          280.  
            init();
          281.  
            });
          282.  
            };
          283.  
             
          284.  
            reader.readAsArrayBuffer(files[0]);
          285.  
            }
          286.  
             
          287.  
            function addTree(scene, uniforms, totalPoints, treePosition) {
          288.  
            const vertexShader = `
          289.  
            attribute float mIndex;
          290.  
            varying vec3 vColor;
          291.  
            varying float opacity;
          292.  
            uniform sampler2D tAudioData;
          293.  
            float norm(float value, float min, float max ){
          294.  
            return (value - min) / (max - min);
          295.  
            }
          296.  
            float lerp(float norm, float min, float max){
          297.  
            return (max - min) * norm + min;
          298.  
            }
          299.  
            float map(float value, float sourceMin, float sourceMax, float destMin, float destMax){
          300.  
            return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
          301.  
            }
          302.  
            void main() {
          303.  
            vColor = color;
          304.  
            vec3 p = position;
          305.  
            vec4 mvPosition = modelViewMatrix * vec4( p, 1.0 );
          306.  
            float amplitude = texture2D( tAudioData, vec2( mIndex, 0.1 ) ).r;
          307.  
            float amplitudeClamped = clamp(amplitude-0.4,0.0, 0.6 );
          308.  
            float sizeMapped = map(amplitudeClamped, 0.0, 0.6, 1.0, 20.0);
          309.  
            opacity = map(mvPosition.z , -200.0, 15.0, 0.0, 1.0);
          310.  
            gl_PointSize = sizeMapped * ( 100.0 / -mvPosition.z );
          311.  
            gl_Position = projectionMatrix * mvPosition;
          312.  
            }
          313.  
            `;
          314.  
            const fragmentShader = `
          315.  
            varying vec3 vColor;
          316.  
            varying float opacity;
          317.  
            uniform sampler2D pointTexture;
          318.  
            void main() {
          319.  
            gl_FragColor = vec4( vColor, opacity );
          320.  
            gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
          321.  
            }
          322.  
            `;
          323.  
            const shaderMaterial = new THREE.ShaderMaterial({
          324.  
            uniforms: {
          325.  
            ...uniforms,
          326.  
            pointTexture: {
          327.  
            value: new THREE.TextureLoader().load(`https://assets.codepen.io/3685267/spark1.png`)
          328.  
            }
          329.  
            },
          330.  
             
          331.  
             
          332.  
            vertexShader,
          333.  
            fragmentShader,
          334.  
            blending: THREE.AdditiveBlending,
          335.  
            depthTest: false,
          336.  
            transparent: true,
          337.  
            vertexColors: true
          338.  
            });
          339.  
             
          340.  
             
          341.  
            const geometry = new THREE.BufferGeometry();
          342.  
            const positions = [];
          343.  
            const colors = [];
          344.  
            const sizes = [];
          345.  
            const phases = [];
          346.  
            const mIndexs = [];
          347.  
             
          348.  
            const color = new THREE.Color();
          349.  
             
          350.  
            for (let i = 0; i < totalPoints; i++) {
          351.  
            const t = Math.random();
          352.  
            const y = map(t, 0, 1, -8, 10);
          353.  
            const ang = map(t, 0, 1, 0, 6 * TAU) + TAU / 2 * (i % 2);
          354.  
            const [z, x] = polar(ang, map(t, 0, 1, 5, 0));
          355.  
             
          356.  
            const modifier = map(t, 0, 1, 1, 0);
          357.  
            positions.push(x + rand(-0.3 * modifier, 0.3 * modifier));
          358.  
            positions.push(y + rand(-0.3 * modifier, 0.3 * modifier));
          359.  
            positions.push(z + rand(-0.3 * modifier, 0.3 * modifier));
          360.  
             
          361.  
            color.setHSL(map(i, 0, totalPoints, 1.0, 0.0), 1.0, 0.5);
          362.  
             
          363.  
            colors.push(color.r, color.g, color.b);
          364.  
            phases.push(rand(1000));
          365.  
            sizes.push(1);
          366.  
            const mIndex = map(i, 0, totalPoints, 1.0, 0.0);
          367.  
            mIndexs.push(mIndex);
          368.  
            }
          369.  
             
          370.  
            geometry.setAttribute(
          371.  
            "position",
          372.  
            new THREE.Float32BufferAttribute(positions, 3).setUsage(
          373.  
            THREE.DynamicDrawUsage));
          374.  
             
          375.  
             
          376.  
            geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
          377.  
            geometry.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
          378.  
            geometry.setAttribute("phase", new THREE.Float32BufferAttribute(phases, 1));
          379.  
            geometry.setAttribute("mIndex", new THREE.Float32BufferAttribute(mIndexs, 1));
          380.  
             
          381.  
            const tree = new THREE.Points(geometry, shaderMaterial);
          382.  
             
          383.  
            const [px, py, pz] = treePosition;
          384.  
             
          385.  
            tree.position.x = px;
          386.  
            tree.position.y = py;
          387.  
            tree.position.z = pz;
          388.  
             
          389.  
            scene.add(tree);
          390.  
            }
          391.  
             
          392.  
            function addSnow(scene, uniforms) {
          393.  
            const vertexShader = `
          394.  
            attribute float size;
          395.  
            attribute float phase;
          396.  
            attribute float phaseSecondary;
          397.  
            varying vec3 vColor;
          398.  
            varying float opacity;
          399.  
            uniform float time;
          400.  
            uniform float step;
          401.  
            float norm(float value, float min, float max ){
          402.  
            return (value - min) / (max - min);
          403.  
            }
          404.  
            float lerp(float norm, float min, float max){
          405.  
            return (max - min) * norm + min;
          406.  
            }
          407.  
            float map(float value, float sourceMin, float sourceMax, float destMin, float destMax){
          408.  
            return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
          409.  
            }
          410.  
            void main() {
          411.  
            float t = time* 0.0006;
          412.  
            vColor = color;
          413.  
            vec3 p = position;
          414.  
            p.y = map(mod(phase+step, 1000.0), 0.0, 1000.0, 25.0, -8.0);
          415.  
            p.x += sin(t+phase);
          416.  
            p.z += sin(t+phaseSecondary);
          417.  
            opacity = map(p.z, -150.0, 15.0, 0.0, 1.0);
          418.  
            vec4 mvPosition = modelViewMatrix * vec4( p, 1.0 );
          419.  
            gl_PointSize = size * ( 100.0 / -mvPosition.z );
          420.  
            gl_Position = projectionMatrix * mvPosition;
          421.  
            }
          422.  
            `;
          423.  
             
          424.  
            const fragmentShader = `
          425.  
            uniform sampler2D pointTexture;
          426.  
            varying vec3 vColor;
          427.  
            varying float opacity;
          428.  
            void main() {
          429.  
            gl_FragColor = vec4( vColor, opacity );
          430.  
            gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
          431.  
            }
          432.  
            `;
          433.  
            function createSnowSet(sprite) {
          434.  
            const totalPoints = 300;
          435.  
            const shaderMaterial = new THREE.ShaderMaterial({
          436.  
            uniforms: {
          437.  
            ...uniforms,
          438.  
            pointTexture: {
          439.  
            value: new THREE.TextureLoader().load(sprite)
          440.  
            }
          441.  
            },
          442.  
             
          443.  
             
          444.  
            vertexShader,
          445.  
            fragmentShader,
          446.  
            blending: THREE.AdditiveBlending,
          447.  
            depthTest: false,
          448.  
            transparent: true,
          449.  
            vertexColors: true
          450.  
            });
          451.  
             
          452.  
             
          453.  
            const geometry = new THREE.BufferGeometry();
          454.  
            const positions = [];
          455.  
            const colors = [];
          456.  
            const sizes = [];
          457.  
            const phases = [];
          458.  
            const phaseSecondaries = [];
          459.  
             
          460.  
            const color = new THREE.Color();
          461.  
             
          462.  
            for (let i = 0; i < totalPoints; i++) {
          463.  
            const [x, y, z] = [rand(25, -25), 0, rand(15, -150)];
          464.  
            positions.push(x);
          465.  
            positions.push(y);
          466.  
            positions.push(z);
          467.  
             
          468.  
            color.set(randChoise(["#f1d4d4", "#f1f6f9", "#eeeeee", "#f1f1e8"]));
          469.  
             
          470.  
            colors.push(color.r, color.g, color.b);
          471.  
            phases.push(rand(1000));
          472.  
            phaseSecondaries.push(rand(1000));
          473.  
            sizes.push(rand(4, 2));
          474.  
            }
          475.  
             
          476.  
            geometry.setAttribute(
          477.  
            "position",
          478.  
            new THREE.Float32BufferAttribute(positions, 3));
          479.  
             
          480.  
            geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
          481.  
            geometry.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
          482.  
            geometry.setAttribute("phase", new THREE.Float32BufferAttribute(phases, 1));
          483.  
            geometry.setAttribute(
          484.  
            "phaseSecondary",
          485.  
            new THREE.Float32BufferAttribute(phaseSecondaries, 1));
          486.  
             
          487.  
             
          488.  
            const mesh = new THREE.Points(geometry, shaderMaterial);
          489.  
             
          490.  
            scene.add(mesh);
          491.  
            }
          492.  
            const sprites = [
          493.  
            "https://assets.codepen.io/3685267/snowflake1.png",
          494.  
            "https://assets.codepen.io/3685267/snowflake2.png",
          495.  
            "https://assets.codepen.io/3685267/snowflake3.png",
          496.  
            "https://assets.codepen.io/3685267/snowflake4.png",
          497.  
            "https://assets.codepen.io/3685267/snowflake5.png"];
          498.  
             
          499.  
            sprites.forEach(sprite => {
          500.  
            createSnowSet(sprite);
          501.  
            });
          502.  
            }
          503.  
             
          504.  
            function addPlane(scene, uniforms, totalPoints) {
          505.  
            const vertexShader = `
          506.  
            attribute float size;
          507.  
            attribute vec3 customColor;
          508.  
            varying vec3 vColor;
          509.  
            void main() {
          510.  
            vColor = customColor;
          511.  
            vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
          512.  
            gl_PointSize = size * ( 300.0 / -mvPosition.z );
          513.  
            gl_Position = projectionMatrix * mvPosition;
          514.  
            }
          515.  
            `;
          516.  
            const fragmentShader = `
          517.  
            uniform vec3 color;
          518.  
            uniform sampler2D pointTexture;
          519.  
            varying vec3 vColor;
          520.  
            void main() {
          521.  
            gl_FragColor = vec4( vColor, 1.0 );
          522.  
            gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
          523.  
            }
          524.  
            `;
          525.  
            const shaderMaterial = new THREE.ShaderMaterial({
          526.  
            uniforms: {
          527.  
            ...uniforms,
          528.  
            pointTexture: {
          529.  
            value: new THREE.TextureLoader().load(`https://assets.codepen.io/3685267/spark1.png`)
          530.  
            }
          531.  
            },
          532.  
             
          533.  
             
          534.  
            vertexShader,
          535.  
            fragmentShader,
          536.  
            blending: THREE.AdditiveBlending,
          537.  
            depthTest: false,
          538.  
            transparent: true,
          539.  
            vertexColors: true
          540.  
            });
          541.  
             
          542.  
             
          543.  
            const geometry = new THREE.BufferGeometry();
          544.  
            const positions = [];
          545.  
            const colors = [];
          546.  
            const sizes = [];
          547.  
             
          548.  
            const color = new THREE.Color();
          549.  
             
          550.  
            for (let i = 0; i < totalPoints; i++) {
          551.  
            const [x, y, z] = [rand(-25, 25), 0, rand(-150, 15)];
          552.  
            positions.push(x);
          553.  
            positions.push(y);
          554.  
            positions.push(z);
          555.  
             
          556.  
            color.set(randChoise(["#93abd3", "#f2f4c0", "#9ddfd3"]));
          557.  
             
          558.  
            colors.push(color.r, color.g, color.b);
          559.  
            sizes.push(1);
          560.  
            }
          561.  
             
          562.  
            geometry.setAttribute(
          563.  
            "position",
          564.  
            new THREE.Float32BufferAttribute(positions, 3).setUsage(
          565.  
            THREE.DynamicDrawUsage));
          566.  
             
          567.  
             
          568.  
            geometry.setAttribute(
          569.  
            "customColor",
          570.  
            new THREE.Float32BufferAttribute(colors, 3));
          571.  
             
          572.  
            geometry.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
          573.  
             
          574.  
            const plane = new THREE.Points(geometry, shaderMaterial);
          575.  
             
          576.  
            plane.position.y = -8;
          577.  
            scene.add(plane);
          578.  
            }
          579.  
             
          580.  
            function addListners(camera, renderer, composer) {
          581.  
            document.addEventListener("keydown", e => {
          582.  
            const { x, y, z } = camera.position;
          583.  
            console.log(`camera.position.set(${x},${y},${z})`);
          584.  
            const { x: a, y: b, z: c } = camera.rotation;
          585.  
            console.log(`camera.rotation.set(${a},$,${c})`);
          586.  
            });
          587.  
             
          588.  
            window.addEventListener(
          589.  
            "resize",
          590.  
            () => {
          591.  
            const width = window.innerWidth;
          592.  
            const height = window.innerHeight;
          593.  
             
          594.  
            camera.aspect = width / height;
          595.  
            camera.updateProjectionMatrix();
          596.  
             
          597.  
            renderer.setSize(width, height);
          598.  
            composer.setSize(width, height);
          599.  
            },
          600.  
            false);
          601.  
             
          602.  
            }
          603.  
            </script>
          604.  
             
          605.  
            </body>
          606.  
             
          607.  
            </html>
           

          WebSocket:實現實時雙向數據傳輸的Web通信協議

          前端達人

           

          前言

          在當今互聯網時代,實時通信已成為很多應用的需求。為了滿足這種需求,WebSocket協議被設計出來。WebSocket是一種基于TCP議的全雙工通信協議,通過WebSocket,Web應用程序可以與服務器建立持久的連接,實現實時雙向數據輸,提供極低的延遲和高效的數據傳輸。


          WebSocket原理

          • HTTP請求-響應協議

          在理解WebSocket原理之前,我們需要了解HTTP請求-響應協議。HTTP是一種無狀態的請求-響應協議,客戶端通過發送HTTP請求到服務器,服務器接收并處理請求,并返回HTTP響應給客戶端。但是,在傳統的HTTP協議中,客戶端只能發送請求,而服務器只能通過響應來處理客戶端的請求。

          • WebSocket協議

          WebSocket協議是在HTTP協議的基礎上進行擴展的。在建立WebSocket連接時,客戶端首先發送一個HTTP請求到服務器,并將Upgrade頭部字段設置為"websocket",表示希望升級到WebSocket協議。服務器接收到這個請求后,如果支持WebSocket協議,會返回一個狀態碼101 Switching Protocols的HTTP響應,并通過Upgrade頭部字段將連接升級為WebSocket連接。

          升級完成后,客戶端和服務器之間的通信不再遵循HTTP請求-響應模式,而是通過WebSocket協議進行雙向的實時通信??蛻舳撕头掌骺梢灾苯影l送消息給對方,不需要等待對方的請求。


          如何使用WebSocket

          建立WebSocket連接:

          要建立WebSocket連接,需要在客戶端和服務器之間進行系列的握手操作。下面是詳細的代碼教程,示了如何在Web應用程序中建立WebSocket連接。

          在戶端(JavaScript):

          // 創建WebSocket對象并指定服務器地址
          var socket = new WebSocket("ws://example.com/socket");
          
          

          // 監聽連接建立事件
          socket.onopen = function() {
          console.log("WebSocket連接已建立");
          // 在連接建立后,可以發送消息到服務器
          socket.send("Hello Server!");
          };

          
          

          // 監聽接收到服務器發送的消息
          socket.onmessage = function(event) {
          var message = event.data;
          console.log("接收到服務器發送的消息:" + message);
          };

          
          

          // 監聽連接關閉事件
          socket.onclose = function(event) {
          console.log("WebSocket連接已關閉");
          };

          
          

          // 監聽連接錯誤事件
          socket.onerror = function(event) {
          console.error("WebSocket連接錯誤:" + event};

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24

          在服務器端(示例使用Node.js):

          const WebSocket = require("ws");
          
          

          // 創建WebSocket服務器
          const wss = new WebSocket.Server({ port: 8080 });

          
          

          // 監聽連接建立事件
          wss.on("connection", function(socket) {
          console.log("WebSocket連接已建立");

          
          

          // 監聽接收到客戶端發送的消息
          socket.on("message", function(message) {
          console.log("接收到戶端發送的消息:" + message);

          
          
          <span class="token comment">// 向客戶端發送消息</span>
          socket<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">"Hello Client!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          
          
          

          });

          
          

          // 監聽連接關閉事件
          socket.on("close", function() {
          console.log("WebSocket連接已關閉");
          });
          });

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22

          在以上代碼中,客戶端通過創建WebSocket對象,并指定服務器地址"ws://example.com/socket"來建立WebSocket連接。同時,客戶端通過監聽onopen事件,可以在連接建立后發送消息到服務器。服務器端使用WebSocket.Server類創建WebSocket服務器,并監聽"connection事件來處理連接建立后的操作。服務器端通過socket.on(“message”)來監聽接收到客戶端發送的消息,并通過socket.send()向客戶端發送消息。

          數據傳輸:

          建立WebSocket連接后,客戶端和服務器可以通過WebSocket對象進行雙向的實時數據傳輸。下面是一個示例代碼,演示了如何在客戶端和服務器之間進行數據傳輸。

          在客戶端(JavaScript):

          // 發送消息到服務器
          socket.send("Hello Server!");
          
          

          // 監聽接收到服務器發送的消息
          socket.onmessage = function(event) {
          var message = event.data;
          console.log("接收到服務器發送的消息:" + message);
          };

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8

          在服務器端(示例使用Node.js):

          // 向客戶端發送消息
          socket.send("Hello Client!");
          
          

          // 監聽接收到客戶端發送的消息
          socket.on("message", function(message) {
          console.log("接收到客戶端發送的消息:" + message);
          });

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7

          在以上代碼中,客戶端通過調用socket.send()方法將消息發送到服務器,服務器通過socket.send()方法將消息發送到客戶端??蛻舳送ㄟ^監聽socket.onmessage事件來接收服務器發送的消息,服務器通過監聽socket.on("message")事件來接收客戶端發送的消息。

          通過以上代碼示例,你可以詳細了解如何使用WebSocket建立連接并進行數據傳輸。請注意,示例代碼中使用的服務器地址和端口號需要根據實際情況進行修改。同時,你還可以在具體應用中根據需要使用WebSocket的其他方法和事件來實現更復雜的功能。


          WebSocket的真實使用場景

          即時通訊:

          WebSocket非常適合用于即時通訊應用,因為它能夠實現實時雙向通信。以下是一個簡單的即時聊天應用的代碼教程。

          在客戶端(JavaScript):

          // 創建WebSocket對象并指定服務器地址
          var socket = new WebSocket("ws://example.com/socket");
          
          

          // 監聽連接建立事件
          socket.onopen = function() {
          console.log("WebSocket連接已建立");

          
          

          // 監聽文本框輸入,按下Enter鍵時發送消息
          var input = document.getElementById("input");
          input.addEventListener("keyup", function(event) {
          if (event.keyCode === 13) {
          var message = input.value;
          socket.send(message);
          input.value = "";
          }
          });
          };

          
          

          // 監聽接收到服務器發送的消息
          socket.onmessage = function(event) {
          var message = event.data;
          console.log("接收到服務器發送的消息:" + message);

          
          

          // 將接收到的消息顯示在聊天窗口中
          var chatWindow = document.getElementById("chatWindow");
          chatWindow.innerHTML += "<p>" + message + "</p>";
          };

          
          

          // 監聽連接關閉事件
          socket.onclose = function(event) {
          console.log("WebSocket連接已關閉");
          };

          
          

          // 監聽連接錯誤事件
          socket.onerror = function(event) {
          console.error("WebSocket連接錯誤:" + event};

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24
          • 25
          • 26
          • 27
          • 28
          • 29
          • 30
          • 31
          • 32
          • 33
          • 34
          • 35
          • 36

          在服務器端(示例使用Node.js):

          const WebSocket = require("ws");
          
          

          // 創建WebSocket服務器
          const wss = new WebSocket.Server({ port: 8080 });

          
          

          // 監聽連接建立事件
          wss.on("connection", function(socket) {
          console.log("WebSocket連接已建立");

          
          

          // 監聽接收到客戶端發送的消息
          socket.on("message", function(message) {
          console.log("接收到客戶端發送的消息:" + message);

          
          
          <span class="token comment">// 向所有連接的客戶端發送消息</span>
          wss<span class="token punctuation">.</span>clients<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">client</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            client<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          
          
          

          });

          
          

          // 監聽連接關閉事件
          socket.on("close", function() {
          console.log("WebSocket連接已關閉");
          });
          });

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24

          在上述代碼中,客戶端通過創建WebSocket對象連接到服務器。輸入框中的文本框用于錄入要發送的消息,按下Enter鍵時會將消息發送給服務器。服務器接收到消息后,通過遍歷所有連接的客戶端,向每個客戶端發送消息。

          這樣,多個客戶端就可以實時地進行聊天,并且所有的消息都會實時地在各個客戶端之間同步顯示。

          多人協作:

          WebSocket還可用于多人協作應用,讓多個用戶可以實時地協同編輯文檔或畫布。以下是一個簡單的代碼教程。

          在客戶端(JavaScript):

          // 創建WebSocket對象并指定服務器地址
          var socket = new WebSocket("ws://example.com/socket");
          
          

          // 監聽連接建立事件
          socket.onopen = function() {
          console.log("WebSocket連接已建立");

          
          

          // 監聽文本框輸入,按下Enter鍵時發送繪畫指令
          var canvas = document.getElementById("canvas");
          canvas.addEventListener("mousedown", function(event) {
          // 繪畫指令的數據格式可以自定義,這里使用了簡單的示例
          var instruction = {
          type: "draw",
          position: {
          x: event.clientX,
          y: event.clientY
          }
          };
          socket.send(JSON.stringify(instruction));
          });
          };

          
          

          // 監聽接收到服務器發送的消息
          socket.onmessage = function(event) {
          var message = JSON.parse(event.data);
          console.log("接收到服務器發送的消息:" + message);

          
          

          // 根據消息執行相應的操作,示例中處理了繪畫指令
          if (message.type === "draw") {
          var canvas = document.getElementById("canvas");
          var ctx = canvas.getContext("2d");
          ctx.beginPath();
          ctx.arc(message.position.x, message.position.y, 5, 0, 2 * Math.PI);
          ctx.fill();
          }
          };

          
          

          // 監聽連接關閉事件
          socket.onclose = function(event) {
          console.log("WebSocket連接已關閉");
          };

          
          

          // 監聽連接錯誤事件
          socket.onerror = function(event) {
          console.error("WebSocket連接錯誤:" + event};

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24
          • 25
          • 26
          • 27
          • 28
          • 29
          • 30
          • 31
          • 32
          • 33
          • 34
          • 35
          • 36
          • 37
          • 38
          • 39
          • 40
          • 41
          • 42
          • 43
          • 44
          • 45

          在服務器端(示例使用Node.js):

          const WebSocket = require("ws");
          
          

          // 創建WebSocket服務器
          const wss = new WebSocket.Server({ port: 8080 });

          
          

          // 監聽連接建立事件
          wss.on("connection", function(socket) {
          console.log("WebSocket連接已建立");

          
          

          // 監聽接收到客戶端發送的消息
          socket.on("message", function(message) {
          console.log("接收到客戶端發送的消息:" + message);

          
          
          <span class="token comment">// 向所有連接的客戶端發送消息</span>
          wss<span class="token punctuation">.</span>clients<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">client</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            client<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          
          
          

          });

          
          

          // 監聽連接關閉事件
          socket.on("close", function() {
          console.log("WebSocket連接已關閉");
          });
          });

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24

          在上述代碼中,客戶端通過創建WebSocket對象連接到服務器。當鼠標在畫布上按下時,將繪畫指令發送給服務器。服務器接收到繪畫指令后,將指令廣播給所有連接的客戶端,并在各個客戶端上進行繪畫操作。

          這樣,多個用戶就可以實時地協同編輯同一個畫布或文檔,所有的繪畫指令都會即時同步在各個客戶端之間。

          實時數據更新:

          WebSocket還可以用于實時數據更新應用,例如股票交易應用中的實時股票價格更新。以下是一個簡單的代碼教程。

          在客戶端(JavaScript):

          // 創建WebSocket對象并指定服務器地址
          var socket = new WebSocket("鏈接");
          
          

          // 監聽連接建立事件
          socket.onopen = function() {
          console.log("WebSocket連接已建立");
          };

          
          

          // 監聽接收到服務器發送的消息
          socket.onmessage = function(event) {
          var message = JSON.parse(event.data);
          console.log("接收到服務器發送的消息:" + message);

          
          

          // 對接收到的實時數據進行處理
          var stockPriceElement = document.getElementById("stockPrice");
          stockPriceElement.innerText = message.price;
          };

          
          

          // 監聽連接關閉事件
          socket.onclose = function(event) {
          console.log("WebSocket連接已關閉");
          };

          
          

          // 監聽連接錯誤事件
          socket.onerror = function(event) {
          console.error("WebSocket連接錯誤:" + event};

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24
          • 25
          • 26

          在服務器端(示例使用Node.js):

          const WebSocket = require("ws");
          
          

          // 創建WebSocket服務器
          const wss = new WebSocket.Server({ port: 8080 });

          
          

          // 模擬實時股票價格更新
          setInterval(function() {
          var stockPrice = Math.random() * 100;

          
          

          // 向所有連接的客戶端發送實時數據
          wss.clients.forEach(function(client) {
          var data = {
          price: stockPrice
          };
          client.send(JSON.stringify(data));
          });
          }, 2000);

          
          

          // 監聽連接建立事件
          wss.on("connection", function(socket) {
          console.log("WebSocket連接已建立");

          
          

          // 初始化發送實時數據
          var stockPrice = Math.random() * 100;
          var data = {
          price: stockPrice
          };
          socket.send(JSON.stringify(data));

          
          

          // 監聽連接關閉事件
          socket.on("close", function() {
          console.log("WebSocket連接已關閉");
          });
          });

           

          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24
          • 25
          • 26
          • 27
          • 28
          • 29
          • 30
          • 31
          • 32
          • 33
          • 34

          在上述代碼中,客戶端通過創建WebSocket對象連接服務器。服務器使用setInterval函數模擬實時股票價格的更新,并將更新的數據發送給所有連接的客戶端??蛻舳吮O聽接收到服務器發送的消息,并處理接收到的實時數據。

          這樣,在股票交易應用中,多個用戶可以實時地接收和顯示股票價格的更新信息。包括打游戲的時候,隊友之間互相溝通,打字交流,或者走位,放技能等等,都是即時的。


          WebSocket的優勢與局限性

          • WebSocket的優勢:
          • 雙向實時通信:WebSocket提供了雙向的實時信能力,客戶端和服務器可以通過該協議進行雙向數據傳輸,實時反饋更新信息,實現即時通訊、實時數據推送等功能。

          • 較低的延遲:與傳統的HTTP請求相比,WebSocket降低了通信的開銷,減少了傳輸和處理數據的延遲,因此可以更快進行實時數據傳輸。

          • 更高的性能:由于WebSocket使用較少的頭部信息和更有效的消息傳輸格式,因此在相同帶寬下可以傳輸更多的數據,提高了性能和效率。

          • 廣泛的瀏覽器支持:WebSocket是HTML5的一部分,并且得到了大多數現代瀏覽器的支持,因此它可以在各種平臺和設備上使用。

          • 連接保持:與傳統的HTTP請求不同,WebSocket連接保持在建立之后,雙方可以隨時進行數據傳輸,避免了不必要的連接和斷開操作。

          • WebSocket的局限性:
          • 兼容性問題:雖然現代瀏覽器廣泛支持WebSocket,但在某些舊版本或特定設備上可能存在兼容性問題。為了兼容性,可以使用輪訓技術(如長輪詢)作為備選方案。

          • 部署和維護復雜性:WebSocket服務器的設置和配置可能比傳統的Web服務器復雜一些,需要專門的服務器環境和配置。

          • 安全性問題:由于WebSocket是在HTTP協議的基礎上建立的,它們共享相同的安全風險,例如跨站點腳本(XSS)和跨站請求偽造(CSRF)。因此,在使用WebSocket時需要考慮到安全性,并采取適當的安全措施。

          • 擴展問題:WebSocket協議還不支持像HTTP/2那樣的一些高級特性,例如頭部壓縮和流量控制。在某些特殊情況下,可能需要通過其他方式實現這些功能。

          盡管WebSocket具有上述局限性,但它仍然是實時通訊、實時數據傳輸和實時協作等場景下的首選協議,因為它具備了雙向實時通信和較低延遲等一系列的優勢。在開發時,需要根據具體需求和限制,綜合考慮使用WebSocket的適用性。


          結論

          WebSocket是一種能夠提供雙向實時通信的協議,適用于需要實時數據傳輸和雙向通信的場景。它具有較低的延遲、更高的性和廣泛的瀏覽器持等優勢,能夠實現即時通訊、多人協和實時數據更新等功能。

          然而,WebSocket也存在兼容性、部署和維護復雜性、安全性問題以及缺乏一些高級特性等局限性。在開發時,需要仔細考慮具體需求和限制,并必要時采取適當的解決方案。

          總的來說,WebSocket在實時通信和實時數據傳輸方面具有明顯的優勢,是構建現代Web應用的重要工具之一。

          藍藍設計(www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的大數據可視化界面設計、B端界面設計、桌面端界面設計APP界面設計、圖標定制用戶體驗設計、交互設計UI咨詢、高端網站設計、平面設計,以及相關的軟件開發服務,咨詢電話:01063334945。

          關鍵詞:UI咨詢、UI設計服務公司、軟件界面設計公司、界面設計公司、UI設計公司、UI交互設計公司、數據可視化設計公司、用戶體驗公司、高端網站設計公司

          銀行金融軟件UI界面設計、能源及監控軟件UI界面設計、氣象行業UI界面設計軌道交通界面設計、地理信息系統GIS UI界面設計、航天軍工軟件UI界面設計、醫療行業軟件UI界面設計、教育行業軟件UI界面設計企業信息化UI界面設計、軟件qt開發、軟件wpf開發、軟件vue開發

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合