没有使用socket.io发送数据

时间:2015-08-14 05:48:37

标签: node.js socket.io

我必须使用socket.io发送数据。当用户输入他的登录名和密码时,他将重定向到index.ejs并且用户名将被发送到服务器。 route.js(我只提到脚本的一部分而不是洞脚本,以显示当登录和密码正确时,用户获取索引页面并将userobject发送到索引):

    var index = function(req, res, next) {
   if(!req.isAuthenticated()) {
      res.redirect('/signin');
   } else {

      var user = req.user;

      if(user !== undefined) {
         user = user.toJSON();
      }
      res.render('index', {title: 'Home', user: user});
   }
};

app.js:

io.sockets.on('connection', function (socket) {

socket.on("new_user",function(user1){
console.log("a new user is connected",user1);
var current_date=new Date();
var date=current_date.toString();
ex.user_connect(user1,date);
});
});

index.ejs:

    <!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width ,initial-scale=1.0" />
    <title><%= title %></title>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="style/style.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>

</head>
<body>
<nav class="navbar navbar-inverse">
        <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">HomePage</a>
                </div>
            <div>

            <ul class="nav navbar-nav navbar-right">
                <li><a href="#"><%= user.username%></a></li>
                <li><a href="/signout"><span class="glyphicon glyphicon-log-out"></span> Sign out</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="jumbotron">
<div class="row">
<div class="col-sm-6">
<div class="container">
  <h2>Led1</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-default btn-lg" id="led1" align="center">check it</button>

  <!-- Modal -->
  <div class="modal fade" id="model_led1" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header" style="padding:35px 50px;">
          <button type="button" class="close" data-dismiss="modal" id="close">&times;</button>
          <h4><span class="glyphicon glyphicon-lock"></span> Settings</h4>
        </div>
        <div class="modal-body" style="padding:40px 50px;">
          <form role="form">

            <img src="style/images/led.png" id="led" class="img-responsive"/>
              <button  type="button" class="btn btn-success btn-block" id="led1_activ"><span class="glyphicon glyphicon-off"></span> Activate</button>
          <button type="button" class="btn btn-success btn-block" id="led1_desactiv"><span class="glyphicon glyphicon-off"></span> Desactivate</button>
          </form>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal" id="cancel_button"><span class="glyphicon glyphicon-remove"></span> Cancel</button>

        </div>
      </div>

    </div>
  </div> 
</div>
</div>

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost:3000');
    var user1 =  user.username;
    socket.emit('new_user',user1);

</script>

注意:我在导航栏上输入了用户名,但我没有在服务器控制台上获取用户名。

1 个答案:

答案 0 :(得分:1)

Ejs在服务器中渲染所有模板变量,然后将普通html返回给客户端,所以

在您的脚本中,您是否已初始化可变全局用户? :

var user1 = user.username // var user = {username:"foo":} ,something like that before.

如果您尝试从模板变量中获取用户,可以尝试:

var user1= "<%= user.username%>";

这将呈现&lt;%= user.username%&gt;发送到客户端(浏览器)之前的脚本标记