基於 NodeJs 打造 Web 在線聊天室

Socket.IO 簡介與基礎環境搭建

任務時間:10min ~ 20minnode

關於 Socket.IO

Socket.IO 能夠實如今瀏覽器和服務器之間實時雙向通訊,本節課程將詳細介紹 Socket.IO 的搭建與部署,並在結束後完成 Socket.IO Web聊天室,幫助你們快速上手 Socket.IO。git

安裝 NodeJs

在 CentOS 環境中,能夠直接使用 yum 安裝 NodeJsgithub

yum install nodejs -y

安裝 npm

任務時間:10min ~ 15minweb

安裝 npm 包管理器

yum install npm -y

搭建 express 環境

任務時間:5min ~ 6minexpress

建立目錄

建立web環境目錄npm

mkdir www
cd www

初始化環境目錄(回車便可)json

npm init -y

關閉npm的https強制效驗瀏覽器

npm config set strict-ssl false

安裝 express

npm install express --save

搭建拉取 socket.io

任務時間:5min ~ 6min服務器

npm 拉取 socket.io

npm install --save socket.io

安裝 git

yum install git -y

拉取 socket demo

git clone https://github.com/socketio/chat-example.git
mv chat-example/* /root/www/

修改 package.json 文件

  • 定位到 /etc/hosts 文件
{
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "^4.15.2",
    "socket.io": "^1.7.3"
  },
  "scripts": {
    "start": "node index.js"
  }
}

安裝 npm 依賴

npm install

運行 Socket.IO

node index.js

訪問 socket 並測試 demo

訪問 <您的 CVM IP 地址>:3000app

相關文章
相關標籤/搜索