使用ionic3和angular4中的socket.io创建聊天室

时间:2018-06-05 06:46:37

标签: javascript node.js ionic3

我正在使用socket.io开发聊天应用程序。问题是,在创建房间时,会将socket-id添加到房间,然后将MSG发送到房间。第一个MSG被完美地发送到房间,但第二个MSG没有被送到房间。第二个MSG显示在服务器端。

服务器端代码

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Fraction {
private:
    int *numerator;
    int *denominator;
public:
    int getNumerator();
    int getDenominator();
    void reduce();
    int gcd(int, int);
    // constructors
    Fraction();  // default c'tor
    Fraction(int n); // create a fraction of n/1
    Fraction(int n, int m); //  Fraction n/m
    Fraction(Fraction & other);  // copy c'tor

    ~Fraction();  // destructor
    Fraction & operator=(Fraction & rhs);
    // overload assignment operator
    Fraction & operator+(Fraction &rhs);
    Fraction & operator-(Fraction &rhs);  // overload binary operator -
    Fraction & operator-();  // overload unary operator -   (negative)
    Fraction & operator *(Fraction &rhs);
    Fraction & operator/(Fraction & rhs);

    Fraction & operator++();// overload prefix ++
    Fraction & operator++(int);  // overload postfix ++
    Fraction & operator--();// overload prefix --
    Fraction & operator--(int);  // overload postfix --

                                 //  overload relational operators
    bool operator >(Fraction & rhs); // return true if *this > rhs , false elsewise
    bool operator == (Fraction & rhs);
    bool operator < (Fraction & rhs);
    bool operator !=(Fraction &rhs);

    Fraction & operator+=(Fraction & rhs);
    Fraction & operator-=(Fraction & rhs);
    Fraction & operator*=(Fraction & rhs);
    Fraction & operator/=(Fraction & rhs);

    string toString();
    char * toCstring();
    bool isZero();  // return true if *this is zero

    int power(int base, int exp);
    Fraction & operator^(int n);

    friend istream & operator >> (istream & in, Fraction & rhs);
    friend ostream & operator << (ostream & out, Fraction & rhs);

};
#endif

加入会议室的客户端代码

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);


io.on('connection', function(socket) {
   console.log('A user connected');

   socket.on('subscribe', function(room) {
       var romSocketId = room.sid;
        socket.to(romSocketId).join(room.roomName);
      //  roome = room;
        console.log('user joined room ' + room);
    });

socket.on('chatmessage',function(data1){
         console.log(data1.msg);
         console.log(data1.room);
         socket.broadcast.to(data1.room).emit('msgFromSever', {message: data1.msg});
});

socket.on("msgToClient", function(data) {
  var sidd = data.sid;
  console.log(sidd);
  // sending to individual socketid (private message)
  socket.to(sidd).emit('msgFromSever', {message: data.msg, socket_id: socket.id, adminID: data.adminLoginId});
    })


socket.on('disconnect', function () {
      console.log('A user disconnected');
   });
});

http.listen(3000, function() {
   console.log('Serve Start');
}); 

发送消息的客户端代码

this.socket.emit('subscribe', {roomName:'room' +this.joinRoomNummber, sid : socketid});

0 个答案:

没有答案