PeerJS 文字聊天 [英] PeerJS text chat

查看:18
本文介绍了PeerJS 文字聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个基于网络的聊天客户端,其中两个人随机连接,然后在没有提示的情况下一起加入聊天.

I am currently writing a web based chat client where two people connect to each other randomly and are thrown into the chat together with no prompting.

我正在使用 PeerJS 库,因为我发现它对我来说是最容易理解的.不过,我处于一个阻塞点:我不确定如何实际实现文本聊天.

I am using the PeerJS library since I found it to be the most understandable one for me. I am at a blocking point though: I am not sure how to actually implement the text chat.

我在他们的网站(这里)上查看了他们的示例,并且我修改了代码有点适合我(主要是它的外观和一些输出是什么).

I have looked at their example on their site (here) and I have modified the code slightly to work for me (mostly just how it looks and what some of the output is).

我遇到的一个问题是,他们的代码需要两个人手动输入对方的 ID,如果没有两个人的验证,聊天将无法开始(也就是说,他们都必须输入 ID 并按下连接"按钮).

A problem I encountered is that their code requires two people to enter the other's id manually and the chat won't start without both people's verification (meaning, they both have to enter the id and press a "Connect" button).

下面是我脑子里想出来的一些代码:

Below is some code I have ran through my head to try to work out:

首先我必须监听与 PeerJS 的连接:

First I have to listen for the connection with PeerJS:

myPeer.on("connection", function() {...});

但同时,我想监听在同一个客户端上打开连接的情况,因为我不想提示用户输入彼此的 ID:

But at the same time, I want to listen for opening a connection on the same client, since I don't want to have to prompt the users for each other's ids:

myConn.on("open", function() {...})

但这是不可能的(据我所知),因为在我打开与上面的 myPeer 侦听器的连接之前,我不知道 myConn 将是什么.

But this is not possible (as far as I know) since I can't know what myConn will be until I open a connection with the myPeer listener above.

我的问题是我不知道如何同时进行.如果它有帮助,我正在尝试与此一起进行视频聊天,我已经想通了,所以如果我能以某种方式搭载该连接或其他任何东西,那也可以.我对项目拥有完全控制权,因此我几乎可以做任何我想做的事情(包括切换库,如果一个库更易于使用或开发得更好).

My problem is that I don't know how to do those simultaneously. If it helps at all, I am attempting to do video chat alongside this, which I have all figured out so if I could somehow piggyback off of that connection or whatever, that works too. I have total control over the project so I can pretty much do whatever I want (including switch libraries, if one is either easier to use or more developed).

推荐答案

connection 事件为您提供了一个 DataConnection 实例.

the connection event gives you a DataConnection instance.

在接收端考虑如下代码:

Consider the following code on the receiving end:

var peer = new Peer('clientID');
peer.on('connection', function(con){
    con.on('data', function(data){
        console.log('Incoming data', data);
        con.send('REPLY');
    });
});

现在在发送端使用这个代码:

Now use this code on the sending end:

var peer = new Peer();
var con = peer.connect('clientID');
con.on('data', function(data){ ... });
con.send('HELLO WORLD');

在发送消息之前等待连接打开很重要.对于上述情况,您可以使用 con.on('open', function(){ ... });

Its important to wait for the connections to be open before sending messages. For the above you could use the con.on('open', function(){ ... });

DataConnections 的行为类似于 TCP 套接字.如果您想要双向通信,您可以使用来自 2 个对等方的 2 个套接字.由于这个问题已经有几个月的历史了,所以我现在要离开双插槽解决方案.如果您还需要它,请告诉我.

DataConnections behave similar to TCP sockets. If you want two-way communication you can use 2 sockets from 2 peers. Since this question is a couple of months old I'm leaving the double socket solution for now. Let me know if you still need it.

这篇关于PeerJS 文字聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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