我无法从数据库中获取更新的数据,尽管它们位于数据库中

时间:2018-12-23 19:10:36

标签: javascript socket.io

通过按一个按钮,我在服务器上发送数据,然后发送到数据库。信息出现在数据库中,但是我需要重新请求从数据库返回所有数据。但是它只返回服务器启动时的信息。要获取我在消息中发送的所有信息,您需要重新启动服务器。工作的逻辑是什么?如何在不重新启动服务器的情况下获取更新的数据?

app.js

io.on('connection', async function (socket) {
var name = 'U' + (socket.id).toString().substr(1, 4);
socket.emit('allMessage', await con.then(function (res) {

     return res;
 }));


socket.broadcast.emit('newUser', name);

socket.emit('userName', name);


socket.on('message', async function (msg) {
    InData.insert(msg, name, "Sex");
    socket.emit('messageToclient',await con.newSelect.then(function (res) {
        return res;
    }));

});
});

客户端的main.js

var SelectChat = angular.module('SelectChat', 
['ngRoute']).config(['$routeProvider',function($routeProvider){
$routeProvider.when("/",{
    templateUrl: 'index.html',
    controller: 'NameInfo'
});
}]).factory('socket',['$rootScope',function ($rootScope) {
var port = 8080;
var socket = io.connect('http://localhost:' + port);

return {
    on: function(eventName, callback) {
        socket.on(eventName,function(data){
            $rootScope.$apply(function(){callback(data)});
        });
    },
    emit: function(eventName,data){
        socket.emit(eventName,data);
        console.log(data);
    }
  }
}]);
SelectChat.controller('NameInfo',function($scope,socket){


socket.on('userName',function(userName){
$scope.name = userName;
});

socket.on('newUser',function(newUser){
//$scope.newUsername = newUser;
});

socket.on('allMessage',function(date){
    $scope.text = date.rows;
});

socket.on('messageToclient',function(date){

$scope.text = date.rows;
console.log(date.rows);

});

$(document).on('click','button',function(){

    var message = $('input').val();
    socket.emit('message',message);
    $('input').val(null);
 });

});

index.html

<html>
<head>
<meta charset="utf-8">
<title>chat</title>
<script src="javascripts/socket.io.js"></script>
<script src="javascripts/jquery.js" charset="utf-8"></script>
<script src="javascripts/angular.js"></script>
<script src="javascripts/angular-route.js"></script>

<script src="javascripts/main.js" charset="utf-8"></script>
<link href="stylesheets/main.css" rel="stylesheet">
<!-- Здесь можете подключить ваш main.css -->
</head>
 <body >
 <header >
 <h1>Chat</h1>
 </header>

 <main ng-app="SelectChat">

<div class="success" ng-controller="NameInfo">
<p>{{name}}</p>
<p ng-repeat="texts in text">{{texts.text}}/</p>

 </div>

</main>
<footer>
<input type="text" name="text" size="100">
<button type="button" name="button" id="sendMsg">Отправить</button>
</footer>

</body>



</html>

2 个答案:

答案 0 :(得分:0)

检查您的代码。您在io回调函数中使用的是“日期”而不是“数据”。

将其更改为此

 Socket.on('allMessages', function(data){
  $scope.text = data.rows;
 })

答案 1 :(得分:0)

Добавил广播ивседовольны)

 socket.broadcast.emit('messageToClient', await con.SelMsg());


          socket.on('message', async function (msg) {
              await InData.insert(msg, name, "Sex");
              socket.emit('messageToClient', await con.SelMsg());
        socket.broadcast.emit('messageToClient', await con.SelMsg());
    });