gpt4 book ai didi

javascript - socket.io 立即启动并再次停止

转载 作者:行者123 更新时间:2023-11-28 20:22:17 26 4
gpt4 key购买 nike

我正在使用node.js 和socket.io。我必须文件:

服务器端:

var http = require('http');
var express = require('express');
var socket = require('socket.io');

var app = express();
var server = http.createServer(app)
var io = socket.listen(server);

io.sockets.on('connect', function (client) {
console.log('Client connected');
client.emit('messages', {hello: "hello"});
});

客户端:

<script src="/socket.io/socket.io.js"></script>

<script>
var server = io.connect('http://127.0.0.1:8000');
server.on('messages', function(data){
alert(data.hello);
});
</script>

当我输入node <name-of-my-file>时发生这种情况:

enter image description here

如有任何建议,我们将不胜感激!

最佳答案

您正在使用expressjs,但没有将其设置为监听任何端口。

var app = express();
var server = http.createServer(app)
app.listen(8000);
var io = socket.listen(server);

关于javascript - socket.io 立即启动并再次停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com