LiveQuery无法使用Node.js和Oriento

时间:2016-10-11 22:52:36

标签: node.js socket.io orientdb oriento

我做了一个 LiveQuery ,当我向类买家插入内容时,我不明白为什么它没有回复。

这是我的代码:

function hearingQueries(){
    server.liveQuery("LIVE SELECT FROM Buyers")
    .on('live-insert', function(data){
      io.emit('chat message', "Server says: " + data.content.message);
    })
    .on('live-delete', function(data){
      io.emit('chat message', "Server deleted: " + data.content.message);
    })
  }

当有人连接到该页面时,函数hearingQuieries()会被呼叫:

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

  server.open();
  hearingQueries();

  console.log('Connection Succed.');

server.open();是一个启动连接的函数。

我的问题是:

- 为什么这不起作用?

- 当有人连接或服务器上线时启动服务器会更好吗?

- 如果在服务器启动时启动它会更好,它不会使服务器变慢吗?

这是我的代码,因此您可以准确了解正在发生的事情:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var OrientDB = require('orientjs').ODatabase;
/*if (typeof localStorage === "undefined" || localStorage === null) {
  var LocalStorage = require("node-localstorage").LocalStorage;
  localStorage = new LocalStorage('./scratch');
}*/


app.get('/indexStyle.css', function(req, res) {

    res.sendFile(__dirname + "/indexStyle.css");
});

app.get('/chat', function(req, res) {
    res.sendFile(__dirname + "/index.html");
});

app.get('/login.css', function(req, res) {
    res.sendFile(__dirname + "/login.css");
});

app.get('/', function(req, res){

    res.sendFile(__dirname + "/login.html");
    console.log('User has reached the login page.');
});

var msgs = [];
var names = [];
var i = 0;

var server = new OrientDB({
    host:       'localhost',
    port:       8081,
    username:   'root',
    password:   'rootPassword',
    name: 'DemoUdemy',
    useToken: true
  });


  function openServer(){
    server.open().then(function(){
      console.log("Server Opened.");
    }); 
  }

  function closeServer(){
    server.close().then(function(){
      console.log("Server Closed.");
    });
  }

  function insert(className, name, bucket, color){

    server.query("INSERT INTO "+className+"(name, bucket, color) VALUES('"+name+"', '"+bucket+"', '"+color+"')");
  }

  /*function select(items ,className){

    var msgTemp = server.query("SELECT '"+items+"' FROM '"+className+"'");
    console.log(msgTemp.Content.Data);
  }*/

  function hearingQueries(){
    server.liveQuery("LIVE SELECT FROM Buyers")
    .on('live-insert', function(data){
      io.emit('chat message', "Server says: " + data.content.message);
    })
    .on('live-delete', function(data){
      io.emit('chat message', "Server deleted: " + data.content.message);
    })
  }

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

  openServer();
  //select("name, bucket", "Buyers");
  hearingQueries();

  console.log('Connection Succed.');

  var iTemp = i - 1;

  for(var msg in msgs)
    socket.emit('chat message', msgs[msg]);

  socket.on('chat message', function(msg){
    msgs.push(names[iTemp] + msg);
    io.emit('chat message', names[iTemp] + msg);
  });

  socket.on('getUser', function(user){
    i++;
    names[i-1] = user + ": ";
  });

  socket.on('disconnect', function(){
    i--;
    closeServer();
    console.log("User has disconnected.");
  });

});

http.listen(8080, function(){
  console.log('Listening to Port *8080');
});

0 个答案:

没有答案