【发布时间】:2017-12-21 09:48:30
【问题描述】:
当我使用socket.io 在客户端建立套接字连接时,我需要将数据发送到我的express 应用程序。但是,当我尝试执行此操作时,我在下面的 active.ejs 文件上收到了一个参考错误。但是,我已成功连接到套接字,因为套接字 ID 已在 index.js 中记录到我的控制台(这也是我需要发送 user_info 数据的地方)
这是客户端 html(ejs) 页面 - active.ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Active</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://localhost:4000/socket.io/socket.io.js"></script>
</head>
<body>
<p>
Welcome,
<%= user_info.uri %>
</p>
// REFERENCE ERROR HERE
// changing {query: user_info} to just user_info doesn't help either
<% var socket = io.connect('http://localhost:4000/tune-in', {query: user_info}); %>
</body>
</html>
user_info 对象是这样从 controller.js 传递到上面的 html(ejs) 文件 active.ejs
app.get('/tune-in', function(req, res){
res.render('active', {user_info: user_data});
});
我需要将user_info 从 active.ejs 发送到 index.js
这是相关的 index.js
var server = app.listen(process.env.PORT || 4000);
console.log("Listening to port 4000...");
var io = socket(server);
io.on('connection', function(socket){
// The below log executes and displays a socket id.
console.log('Made socket connection', socket.id);
});
【问题讨论】:
标签: javascript node.js express socket.io ejs