[英]socket.emit not passing messages
我有以下js代码
`var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require("path");
var io = require('socket.io')(http);
app.get('*', function (req, res){
res.sendFile(path.join(__dirname, '/Public'));
});
app.use('/home',express.static(path.join(__dirname,'/Public')));
//app.use('/static', express.static(__dirname + 'index.html'));
io.on('connection', function (socket) {
socket.on('message', function (data) {
socket.emit('news', { hello: 'world' });
});
socket.on('another-message', function (data) {
socket.emit('not-news', { hello: 'world' });
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});`
我有HTML代码
<html>
<h1>working</h1>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000', { path :'/' +home});
socket.on('connect',function(){
socket.emit('message', 'Hello server');
});
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
<body>
<p>display a message</p>
</body>
</html>`
我转到页面localhost:3000 / home,我得到了HTML页面。 但是在控制台中,我看不到任何消息。 我哪里错了? 请纠正我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.