简体   繁体   English

使用NodeJS / socketio记录来自HTML输入的消息

[英]Log a message from HTML input with NodeJS/socketio

I'd like it so whenever I type something in a input box and click send it logs in the NodeJS console what has been send, for example a user types "Hello world" and then clicks "Send message to console" and then in the console it will say "Hello world". 我想这样,每当我在输入框中键入内容并单击“发送”,就会在NodeJS控制台中记录已发送的内容,例如,用户键入“ Hello world”,然后单击“将消息发送到控制台”,然后在控制台将显示“ Hello world”。

I have really no idea on how I should do this 我真的不知道该怎么做

You should read socketio doc: http://socket.io/docs/ 您应该阅读socketio doc: http ://socket.io/docs/

On the client side you send the data like this: 在客户端,您将像这样发送数据:

socket.emit('inputbox', data)

On the server side inside the "io.on" callback you use: 在服务器端的“ io.on”回调中,您可以使用:

socket.on('inputbox', function (data) {
    console.log(data)
  })

There is also a very detailed tutorial that should get you started: http://socket.io/get-started/chat/ 还有一个非常详细的教程应该可以帮助您入门: http : //socket.io/get-started/chat/

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

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