无法运行基于套接字的nodejs客户端聊天。

问题描述 投票:0回答:1

我按照一个tuto,试图做一个简单的nodejs cli套接字聊天室。

聊天节点.js

const http = require('http').createServer();
const io = require('socket.io')();
const port = 4400

io.on('connection', (socket) => {
  console.log('connected')
  socket.on('message', (evt) => {
      console.log(evt)
      socket.broadcast.emit('message', evt)
  })
})
io.on('disconnect', (evt) => {
    console.log('disconnected')
})
http.listen(port, () => console.log(`server listening on port: ${port}`))

聊天客户端.js

var socket = require('socket.io-client')('http://localhost:4400');
const repl = require('repl')
const chalk = require('chalk');
socket.on('disconnect', function() {
    socket.emit('disconnect')
});
socket.on('connect', () => {
    console.log('=== start chatting ===')
})
socket.on('message', (data) => {
    const { cmd } = data
    console.log(chalk.green(cmd.split('\n')[0]));
})
repl.start({
    prompt: '',
    eval: (cmd) => {
        socket.send(cmd)
    }
})

当我启动 node chat-node 命令,服务器输出监听端口XXXX,但当我执行 node chat-client 命令的执行没有任何错误,但是控制台没有打印,是空白的。开始聊天的字符串也没有被打印出来。

node.js sockets socket.io client command-line-interface
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.