を送信し、ここで私のserver.jsSocket.ioは、私がセットアップsocket.ioにしようとしている二つのメッセージ
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http, { path: '/websocket', origins:'*:*' });
io.on('connection', (socket) => {
socket.send('Hi');
socket.on('message', (message) => {
console.log(message);
socket.emit('hello', `New: ${message}`);
});
console.log('a user connected');
});
http.listen(3030, function(){
console.log('listening on *:3030');
});
を、私の単純なクライアントの一部です:
var socket = io('https://*******.com', {
secure: true,
path: '/websocket'
});
const input = document.getElementById('text');
const button = document.getElementById('button');
const msg = document.getElementById('msg');
button.onclick =() => {
socket.emit('message', input.value);
socket.on('hello', (text) => {
const el = document.createElement('p');
el.innerHTML = text;
msg.appendChild(el);
})
}
3回目にクリックすると3つのメッセージが返ってきます。私は間違っているの?私はサーバーにメッセージを送って、変更されたメッセージを返信したいと思います。 私はウェブソケットで新しいです。
助けてください。
P.S. socket.io v2.0.1
ありがとうございました!あなたは私の日を救う。 –