Javascript 为什么它在控制台中没有回声
我只是从其他网站上了解了这一点,我还是socket.io和node.js的新手。我不明白为什么它没有在控制台中回音。有人能帮我吗 先谢谢你 app.jsJavascript 为什么它在控制台中没有回声,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我只是从其他网站上了解了这一点,我还是socket.io和node.js的新手。我不明白为什么它没有在控制台中回音。有人能帮我吗 先谢谢你 app.js var server = require("net").createServer(); var io = require("socket.io")(server); var handleClient = function (socket) { // we've got a client connection socket.em
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>
$(函数(){
var iosocket=io.connect(“http://localhost");
iosocket.on(“tweet”,函数(tweet){
//todo:将tweet添加为DOM节点
日志(“内容:”,tweet.text);
});
});
你是说tweet.user
而不是.username
?试试io.connect(“http://localhost:8080");
@Bergi Bingo:-)和OP使用端口号io.connect(“)或简单地使用io.connect()@Bergi,不工作我尝试了io.connet();或io.connect(”http://localhost:8080“”;
它仍然没有回音,我把tweet.username注释掉。@jemz:squiroid应该是对的,试试io.connect(“localhost:8080”)
或io.connect(“ws://localhost:8080“
-您没有使用http