Parcourir la source

增加socket.io支持;js使用ECMA6,增加class支持

Sand Wen il y a 8 ans
Parent
commit
e698052487

+ 1 - 0
readme.md

@ -11,6 +11,7 @@
- 开发工具: node.js的代码可以使用文本编辑器开发,如:Sublime, vi, vim等。也可以使用IDE开发,推荐使用WebStorm,包含大量便捷功能,加速开发。
- 数据库准备: IM服务器使用MySQL数据库,因此需要先安装MySQL数据库。然后执行resources/schema/im_schema.sql与resources/schema/talk_group_schema.sql脚本文件,创建数据模式。第一个创建消息存储所需要的模式,第二个创建业务讨论组数据模式,并包含演示数据。
- 运行环境:node.js使用社区6.X版本或Joyent的0.10.x版本开发,部署环境也需要使用相应的版本。
- ECMA版本:代码需要ECMA 6版本的支持,提供对class, arrow function的支持。
## 运行

+ 0 - 14
src/doctor/handlers/PatientClient.js

@ -1,14 +0,0 @@
/**
 *
 * author: Sand
 * since: 2016/11/18
 */
"use strict";
function PatientClient(socket) {
    this.id = null;
    this.password = null;
    this.socket = module.exports;
}
module.exports = PatientClient;

+ 22 - 9
src/doctor/handlers/socket.handler.js

@ -1,21 +1,34 @@
/**
 * Created by Sand on 2016/11/17.
 * author: Sand
 * since: 2016/11/17
 */
"use strict";
var log = require("../util/log.js");
var PatientClient = require('./PatientClient');
var clients = {};
var clientCache = require('../models/socket.io/client.cache').clientCache();
var PatientClient = require('./../models/socket.io/patient.client');
module.exports.onConnection = function(socket){
    log.info("Patient connected from WebSocket.");
/**
 * 建立WS连接,发送welcome消息要求登录。之后响应login消息保存用户通信凭据。
 *
 * @param socket
 */
module.exports.onConnection = function (socket) {
    socket.on('login', function (data) {
        log.info('User login: ' + data.userId);
    socket.on('data', function (data) {
        var patientClient = new PatientClient(socket);
        patientClient.userId = data.userId;
        patientClient.password = data.password;
        clientCache.addClient(patientClient);
    });
    socket.emit('welcome', {message: 'Welcome to IM server.'});
};
module.exports.onDisconnect = function () {
    socket.on('disconnect', function () {
        log.info("User disconnect: ", clientCache.findBySocket(socket).userId);
        clientCache.removeByUserSocket(socket);
    });
    socket.emit('welcome', {message: 'Welcome to connect IM server, please login.'});
};

+ 61 - 0
src/doctor/models/socket.io/client.cache.js

@ -0,0 +1,61 @@
/**
 * 客户端缓存池,记录各用户的socket与相应的client.
 *
 * author: Sand Wen
 * since: 2016.11.19
 */
"use strict";
var log = require('../../util/log');
var clientCache = null;
class ClientCache {
    constructor(){
        this._clientsBySocket = new Map();
        this._clientsByUserId = new Map();
    }
    get clients(){
        return this._clientsByUserId;
    }
    addClient(client){
        this._clientsByUserId.set(client.userId, client);
        this._clientsBySocket.set(client.socket, client);
        log.info('Current clients: ', this.clients);
    }
    removeByUserId(userId){
        var socket = this.findById(userId);
        this._clientsByUserId.delete(userId);
        this._clientsBySocket.delete(socket);
    }
    removeByUserSocket(socket){
        var userId = this.findBySocket(socket);
        this._clientsByUserId.delete(userId);
        this._clientsBySocket.delete(socket);
    }
    findById(userId){
        return this._clientsByUserId.get(userId);
    }
    findBySocket(socket){
        return this._clientsBySocket.get(socket);
    }
    static clientCache(){
        if(clientCache === null){
            clientCache = new ClientCache();
        }
        return clientCache;
    }
}
module.exports = ClientCache;

+ 41 - 0
src/doctor/models/socket.io/client.js

@ -0,0 +1,41 @@
/**
 * socket客户端。
 *
 * author: Sand
 * since: 2016.11.19
 */
"use strict";
class Client{
    constructor(socket) {
        this._socket = socket;
        this._userId = '';
        this._password = '';
    }
    get socket(){
        return this._socket;
    }
    get userId(){
        return this._userId;
    }
    set userId(userId){
        this._userId = userId;
    }
    get password(){
        return this._password;
    }
    set password(password){
        this._password = password;
    }
    tellus(){
        return "Socket: " + this.socket + ", user: " + this.userId + ", password: " + this.password;
    }
}
module.exports = Client;

+ 19 - 0
src/doctor/models/socket.io/patient.client.js

@ -0,0 +1,19 @@
/**
 * 患者端socket.
 *
 * author: Sand
 * since: 2016/11/18
 */
"use strict";
var util = require('util');
var Client = require('./client');
class PatientClient extends Client{
    constructor(socket){
        super(socket);
    }
}
module.exports = PatientClient;

+ 7 - 1
src/doctor/public/html/socket/test.html

@ -11,7 +11,13 @@
    var socket = io.connect();
    socket.on('welcome', function (data) {
        console.log(data.message);
    })
        socket.emit('login', {userId: 'sand', password: 'hello'});
    });
    socket.on('message', function (data) {
        console.log(data.message);
    });
</script>
</body>
</html>