简体   繁体   English

socket.io-client连接,但不发光

[英]socket.io-client connecting, but not emitting

I am making a little encrypted chat app, in the terminal, using socket.io-client and socket.io. 我正在使用socket.io-client和socket.io在终端中制作一个加密的聊天应用程序。 The client is able to connect to the server, but is not emitting the username, when its entered. 客户端能够连接到服务器,但是在输入用户名时不会发出用户名。

Client: 客户:

 var socket = require('socket.io-client')('http://127.0.0.1:3000');
 socket.on('connect_error', function(){
console.log('Failed to establish a connection to the servers, or lost     connection');

 return process.exit();     
  });
 var prompt = require("prompt-sync")()
 var news = "Add news: Will be from database. " 
 var username = prompt("Username>: ")

 console.log("Hold on a sec, just checking that!")
 console.log("")
 if (typeof username === "defined"){
 socket.emit('user-name', {usr: 'username'})
 }


socket.on('user-name-good',function(socket){
console.log("Okay! Your username looks good, we just require your password")
console.log("If you chose to have no password, please press enter with    out pressing space!")
 var password = prompt("Password>: ")
  if (typeof password !== "defined"){
    console.log("Please provide a password!")
    return password = prompt("Username>: ")
 }

 socket.on('user-name-fail',function(socket){
 console.log("Sorry, we could not find, "+username+""+"Please register    on the website, or, if you have registered ")
 return process.exit()
})    

}
)

Server code, is based on code from socket.io chat example: 服务器代码基于socket.io聊天示例中的代码:

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

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

I have added a error event, this closes the client if a connection to the server fails, so I know its connecting, any help appreciated, I have done research on this topic, and tried a lot of other methods, but to no avail. 我添加了一个错误事件,如果与服务器的连接失败,它将关闭客户端,所以我知道它的连接,感谢您的帮助,我已经对该主题进行了研究,并尝试了许多其他方法,但无济于事。

Also the connection is made after you submit data, not when the client code is started, what could be causing this? 另外,连接是在提交数据之后进行的,而不是在启动客户端代码时进行的,这可能是什么原因引起的?

If you want to send events between client and server you have to: 如果要在客户端和服务器之间发送事件,则必须:

Send event A from client to the server and server has to be listening for the A event. 从客户端向服务器发送事件A,服务器必须监听A事件。

If you want to send event B from server to client then client has to be listening for the event B. 如果要将事件B从服务器发送到客户端,则客户端必须监听事件B。

Apart from everything else in your code I don't see where you are listening for the 'chat message' event on the client side. 除了代码中的所有其他内容,我看不到客户端在哪里监听“聊天消息”事件。

Socket.io is based on these so called 'events'. Socket.io基于这些所谓的“事件”。 The code below is going to send 'my_event' event to the server and the trasmitted data is going to be the object { a: 1 }. 下面的代码将向服务器发送'my_event'事件,所传输的数据将成为对象{a:1}。

socket.emit('my_event', { a: 1 });

If I want to handle this event on the server I have to listen for it: 如果要在服务器上处理此事件,则必须侦听它:

socket.on('my_event', function(data) {
    // data is the object { a: 1 }
    // do stuff..
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM