我只是从其他网站https://nodesource.com/blog/understanding-socketio关注这个,我还是在socket.io中使用node.js的新手,。我很困惑为什么它没有在console中回显。有人帮我这个。< / p>
提前谢谢。
app.js
var server = require("net").createServer();
var io = require("socket.io")(server);
var handleClient = function (socket) {
// we've got a client connection
socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};
server.listen(8080);
io.on("connection", handleClient);
的index.html
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(function(){
var iosocket = io.connect("http://localhost");
iosocket.on("tweet", function(tweet) {
// todo: add the tweet as a DOM node
console.log("contents:", tweet.text);
});
});
</script>
</head>
<body>
</body>
</html>