node_5/websocke/socket.io/solve foreign module download problem/mocha/chai (Assertion library)

Source: Internet
Author: User
Tags emit

WebSocket (to go to GitHub to install a WS) client has compatibility: WebSocket ("") only the higher version of the browser has
    1. NPM Init
    2. NPM Install--save ws
WebSocket
  * client.html<! DOCTYPE html> 
* server.jsconst WebSocket = require(‘ws‘);const server = new WebSocket.Server({ port: 8080 });const clients = [];server.on(‘connection‘, (client) => { client.id = clients.length; clients.push(client) client.on(‘message‘, (message) => { //客户端监听 clients.forEach((clientItem) => { if (clientItem) { //clientItem && clientItem !== client可以屏蔽自己 clientItem.send(message); } }); }); client.on(‘close‘, () => { //容错 clients[client.id] = null }); client.on(‘error‘, () => { //容错 clients[client.id] = null });});
Socket.io (access to get started in Socket.io)
    1. NPM init-y
    2. NPM install--save [email protected]
    3. NPM Install--save Socket.io
Socket.io
* Index.html<!doctype html>
* index.jsvar app = require(‘express‘)();var http = require(‘http‘).Server(app); //让用户能够访问一个网页var socketServer = require(‘socket.io‘)(http); //聊天app.get(‘/‘, function(req, res){ res.sendFile(__dirname + ‘/index.html‘); //sendFile是express提供的一个方法;可以把一个文件读取出来后返回给用户 //__dirname是node中的一个全局变量表示当前文件所在的绝对路径});socketServer.on(‘connection‘, function(socket){ //console.log(‘a user connection‘); //一旦有用户连接就输出a user connection socket.on(‘chat‘, function(msg){ //socket.broadcast.emit(‘brodcast‘, msg);//broadcast广播,一旦接收到一个用户的聊天就广播所有用户 socketServer.emit(‘brodcast‘, msg);//让自己接收自己发送的消息 });});http.listen(3000);
Solve foreign module Download problem:
    1. FQ
    2. Switch Source: NRM
      NPM Install Nrm-g
      NRM ls View usage source
      NRM use CNPM switch source to CNPM
Automated Test Framework Mocha (Mocha official website) Karma

Automated testing for large projects without frequent changes, small function iterations are relatively fast and are not recommended to use

    1. Yarn Init-y
    2. NPM Install--global Mocha Global Installation Mocha
    3. Create a new test folder to store the automated test code
    4. The Const ASSERT = require ("Assert") in the JS code under the test folder
    5. Input Mocha will automatically test all programs under the test folder
Test async needs to write a done to the parameter, and then call

MOCHA-T 5000 Asynchronous time-out is 5 seconds

Automated testing facilitates QA but increases front-end public work chai (assertion library) Search Chai There are 3 kinds of syntax
    1. NPM Install Chai--save
Automated Test Framework Mocha
* math.jsmodule.exports = {    add: function(a, b){        return Number(a, 10) + Number(b, 10);    },    minus: function(a, b){        return Number(a, 10) - Number(b, 10);    },    multiply: function(a, b){        return a * b;    },    devision: function(a, b){        return a / b;    }}
  * Math.test.js//const assert = require ("assert")//Assert Const CHAI = require ("Chai")//Assert library const MATH = r Equire ("..      /math.js ") chai.should (); Chai a syntax describe ("I'm going to test the Math module", () = {//describe is used to describe what it does ("Test Add Method", () = {//it description and true test code/*a  Ssert.equal (Math.add (1, 2), 3)//equal represent equality assert.equal (Math.add ( -1, 2), 1) assert.equal (Math.add ("1", 2), 3) */Math.add (1, 2). Should.equal (3)}), it ("Test minus Method", () = {//it description and true test code/*assert.equal (MA         Th.minus (1, 2),-1)//equal means equal assert.equal (Math.minus ( -1, 2), 3) assert.equal (Math.minus ("1", "2"),-1) Assert.equal (typeof Math.minus ("1", "2"), ' number ') */Math.minus (1, 2). Should.equal ( -1)}), it ("Test 2 seconds of , the multiplication is good ", (done) = {SetTimeout () {/*assert.equal (Math.multiply (5, 3), +) */M Ath.multiply (5, 2). Should.equal (Ten) Done ()})})  

node_5/websocke/socket.io/solve foreign module download problem/mocha/chai (Assertion library)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.