简体   繁体   English

如何在客户端管理套接字作为ejs模板和反应js和服务器端它是nodejs

[英]How to manage socket in client side as ejs template and react js and the server side it is nodejs

In my project use the reactJS for frontend and ejs template engine with a node for the backend.在我的项目中,使用 reactJS 作为前端,使用 ejs 模板引擎,后端使用一个节点。 already the project is started so now I can't change the whole project into reactJS.项目已经开始,所以现在我无法将整个项目更改为 reactJS。

creating one chat app with two clients(react and ejs) and one server(nodeJs) but I stuck in message passing to both clients at one time.使用两个客户端(react 和 ejs)和一个服务器(nodeJs)创建一个聊天应用程序,但我一次将消息传递给两个客户端。

my code is below please tell me is that possible to create a chat app with ejs, node, and react.我的代码在下面请告诉我是否可以使用 ejs、node 和 react 创建一个聊天应用程序。

ejs:- 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:- 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)
    }

nodeJS:-节点JS:-

module.exports = function(io) {
    io.on('connection', function(socket){
        socket.emit('receive-user',details);
    })
}

Please Help...请帮忙...

use as per below it will work fine按照下面使用它会正常工作

io.on('connection', function(socket){
   io.sockets.emit('receive-user',details);
})

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

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