使用 socket.io 反应本地一对一对话 [英] React Native one to one conversation using socket.io

查看:27
本文介绍了使用 socket.io 反应本地一对一对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 react native 应用,nodejs express Sequelize 作为我的后端,postgres 作为我的数据库.

i currently have a react native app with nodejs express Sequelize as my backend and postgres as my database.

因此,在每个帖子旁边的帖子屏幕上,我有一个文本输入和一个按钮,当前用户可以在其中向帖子的用户发送初始消息.按下按钮后,这两个用户之间关于此帖子的对话将在我的数据库中创建并存储在我的对话表中,发送的消息条目也存储在我的消息表中.

So, on my posts screen next to each post, i have a text input and a button where the current user can send the user of the post an initial message. Once the button is pressed, a conversation between these 2 users about this post is created in my database and stored in my conversation table and an entry of the message sent is also stored in my messages table.

我已经实现了这两个用户之间的双向通信.但我的问题是我需要刷新应用程序才能向用户当前用户显示发送的消息并向接收用户显示收到的消息.

I have implemented bidirectional communication between these 2 users. But my problem is i need to refresh the app in order to show the user current user the sent message and to show the receiving user the received message.

我已经研究了一段时间,并试图了解如何使用 socket.io 实现此功能,但一无所获.

I have been researching for a while now and trying to understand how to implement this feature using socket.io but could not get anywhere.

客户端

这是我的聊天画面

 function ChatScreen({route,navigation}) {

 const message = route.params.message;
 const [messages, setMessages] = useState(message.Messages);
 const [text, setText] = useState('');
 const { user } = useAuth();
 const [socket, setSocket] = useState(null);


 useEffect(() => {
 const newsocket =io.connect(socketurl)
setMessages(messages);
newsocket.on('connection', msg => {
    console.log('i have joined')
    setMessages(messages=>messages.concat(msg))
    setSocket(newsocket)
 })
 return()=>newsocket.close;
 }, []);

const updateText=(text)=>{
setText(text);
}

  const onSend = (ConversationId,senderId,receiverId,message) => {
  console.log("sent")
messagesApi.sendMessage({ConversationId,senderId,receiverId,message});
setText("")

socket.emit('message', { to: (user.id===route.params.message.user1 ? 
route.params.message.user2 : route.params.message.user1), from: 
user.id, message,ConversationId });
};

return(

<Text>{user.id === message.Recipient.id ? 
message.Creator.name:message.Recipient.name}</Text>

    <KeyboardAvoidingView 
    style={{
        display: "flex",
        flex: 1,
      }}
      behavior={Platform.OS === "ios" ? "padding" : null}
      keyboardVerticalOffset={Platform.OS === "ios" ? 25 : 0}
    >

<FlatList
    inverted
    data={message.Messages}
    keyExtractor={(message) => message.id.toString()}
    renderItem={({item,index})=>(
        <MessageBubble
        text={item.message}
        mine={item.senderId !== user.id}
        />
    )}/>

    <View style={styles.messageBoxContainer}>
        <TextInput 
        style={styles.messageBox} 
        placeholder="Message..." 
        multiline
        clearButtonMode="while-editing"
        onChangeText={updateText} 
        value={text} 
        autoCorrect={false}
        />
        <TouchableOpacity onPress={onSend}>
            <Text style={styles.send}>Send</Text>
        </TouchableOpacity>
    </View>
    </KeyboardAvoidingView>
)

服务端

index.js

const express = require("express");
const app = express();
const http = require("http");
const socketio = require("socket.io")
const server=http.createServer(app);
const io =socketio(server)

io.on("connection", socket => {
 socket.on('message', (data) => {
  socket.join(data.ConversationId);
  io.sockets.in(data.to).emit('send_message', { message: data.message, 
  to: data.to });
});
});

const port = process.env.PORT || config.get("port");
server.listen(port, function () {
console.log(`Server started on port ${port}...`);
});

目前,当我发送消息时,消息会存储在我的数据库中,但我的聊天不会立即更新(即不是实时更新),我需要刷新我的应用程序并显示消息.

Currently when i send a message, the message gets stored in my database but my chat does not update instantly (ie. not live), i need to refresh my app and the messages appear.

有人可以帮助我并检查我当前拥有的套接字的实现是否正确,如果正确,我如何立即呈现我的平面列表?

Can someone please help me and check if the implementation of socket i currently have is correct and if so, how do i render my flatlist instantly?

更新

我认为我的 useEffect 有问题,因为当我打开聊天时,我没有收到我已加入";在控制台中:

i think something is wrong in my useEffect, because when i open the chat i am not getting "i have joined" in the console:

useEffect(() => {
setMessages(messages);
socket.on('connect', msg => {
    console.log('i have joined')
    setMessages(messages=>messages.concat(msg))
 })
 }, []);

推荐答案

Socket Index.js

Socket Index.js

    /** Socket.io server listens to our app **/
    var io = require('socket.io').listen(app);
    
    
    io.on('connection', function (socket) {
    /** On User Log In **/
    socket.on('login', function (data) {
            console.log('user joined >>', data)
            userList.addUser(data, socket);
        });

    // Example Event //
    socket.on('get_online_friends', userId => {
    //Get List Data And Then //
    let data = [UserList];
    socket.emit('send_online_friend', data);
    }
    )
    // On Logout //
     socket.on('disconnect', function (reason) {
            var offlineId = userList.removeUser(socket)
    );
    
    }

user_list.js

user_list.js

    var userList = {};
    module.exports = userList;
    
    var userData = [];
    var userSocData = {};
    
    userList.user = userData;
    userList.userSoc = userSocData;
    
    userList.getUserList = function () {
        return userSocData;
    };
    
    userList.addUser = function (user, client) {
        userSocData[user] = {
            socketId: client.id
        }
    };
    
    userList.setReceiverId = function (user, client) {
        var index = userData.findIndex(x => x.user_id == user['user_id']);
        if (index !== -1) {
            userData[index]['receiver_id'] = user['receiver_id'];
        }
        userSocData[user['user_id']] = {
            socket: client.id
        };
    };
    
    
    userList.removeUser = function (client) {
        for (const property in userSocData) {
            if (client.id === userSocData[property].socketId) {
                var userID = property;
                delete userSocData[property]
            }
        }
        return userID;
    };

前端

    ***
    socket.emit("get_request", userData.user_id);
            socket.on("get_request_data", function (data) {
              if (data.status) {
                self.setState({ onlineFriends: data.data });
              }
            });
    ***

这篇关于使用 socket.io 反应本地一对一对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆