[英]How to manage socket in client side as ejs template and react js and the server side it is nodejs
在我的项目中,使用 reactJS 作为前端,使用 ejs 模板引擎,后端使用一个节点。 项目已经开始,所以现在我无法将整个项目更改为 reactJS。
使用两个客户端(react 和 ejs)和一个服务器(nodeJs)创建一个聊天应用程序,但我一次将消息传递给两个客户端。
我的代码在下面请告诉我是否可以使用 ejs、node 和 react 创建一个聊天应用程序。
ejs:-
<script src="/backend/chat/socketMessage.js"></script>
var socket = io.connect('http://localhost:8080');
socket.on('receive-user',function(data){
console.log('receive on ejs');
})
reactJs:-
import io from 'socket.io-client';
const ENDPOINT = 'localhost:8080';
constructor() {
super();
endpoint:ENDPOINT,
}
const {endpoint} = this.state;
const socket = io(endpoint);
socket.on('receive-user', this._messageRecieve);
_messageRecieve(message) {
console.log(message)
}
节点JS:-
module.exports = function(io) {
io.on('connection', function(socket){
socket.emit('receive-user',details);
})
}
请帮忙...
按照下面使用它会正常工作
io.on('connection', function(socket){
io.sockets.emit('receive-user',details);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.