无线网络连接P2P。告知可用一些事件的所有同行 [英] Wi-fi P2P. Inform all peers available of some event

查看:168
本文介绍了无线网络连接P2P。告知可用一些事件的所有同行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我正在做一个离线多人的Andr​​oid游戏,在这里人们可以创建或加入一个房间,通过Wi-Fi一起玩。审时度势,当用户创建一个房间,他(当然)必须通知所有其他用户有可用的空间。所以,问题是怎么了?我读过约1000倍这个
 和
这个
 这是写有,为了某些数据发送到另一设备,其中之一应该是一个服务器,而另一个客户机。客户端发送一些信息到服务器,服务器接受它。那么,这是否意味着我必须让所有运动员服务器和房间创造者应该成为一个客户端?这听起来很疯狂。请帮帮忙,可能是我读了错误的文档?

The problem: I'm making an offline-multiplayer android game, where people can create or join a room and play together via Wi-fi. Consider the situation when a user creates a room and he (of course) has to inform all other users that there is a room available. So the question is "How?".I've read about 1000 times this and this. It's written there that in order to send some data to another device, one of them should be a server and the other one a client. Client sends some info to the server, server accepts it. So, does it mean that I have to make all "players" servers and the "room creator" should become a client? This sounds crazy. Please help, may be I'm reading the wrong docs?

推荐答案

所有你需要记住,如果你决定使用首先 WiFiP2P 在你的项目,你会需要prepare自己的一些艰难的时刻。可惜的是(因为你已经可能已经注意到:)) WiFiP2P Android的官方文档不是很大。不同设备的行为以不同的方式(例如,它们调用不同的顺序 WifiP2P 事件)和文档不覆盖它。实现稳定的连接需要引入非常规的,有时严厉的措施。获得 WifiP2P 连接inseperably与显示系统窗口(询问您是否真的要连接的设备)链接。

First of all you need to remember that if you do decide to use WiFiP2P in your project you will need to prepare yourself for some difficult time. Unfortunatelly (as you already might have noticed :)) WiFiP2P Android's official documentation is not that great. Different devices behave in different ways (e.g. they call some WifiP2P events in different order) and docs don't cover it at all. Achieving a stable connection requires introducing unconventional and sometimes drastic measures. Obtaining a WifiP2P connection is inseperably linked with displaying a system window (asking if you really want to connect to a device).

有你需要在开始实施 WifiP2P 解决方案之前要考虑几件事情:

There are few things you need to consider before you start implementing a WifiP2P solution:


  1. 您的游戏的玩家将可以同时连接到多个房间?

  2. 您的游戏玩家将能够将新检测室在连接到另一个?

  3. 将你的游戏玩家能够说出它们与人类可读的名字的房间?而这些名字会被其他用户(不是房间的创建者)可以用来选择一个房间,他们想连接到?

如果你回答是对问题1或2,您需要认真考虑,如果你真的想用 WifiP2P WifiP2P (Android的实施的WiFi的直接)并不真正适合您的需求。
使用 WifiP2P 可能会意味着一个房间所有者创建一个 WifiP2P组和谁愿意加入房间必须的任何人连接到该 WifiP2p组
问题是Android不容许一个单一的设备在同一时间加入多个组(这也意味着它不允许为创建多个在同一时间组)。检测可用的 WiFiP2P 设备周围的变化也存在问题(如果您已连接到设备)。

Scenario A:

If you answered "Yes" to question 1. or 2., you seriously need to consider if you really want to use WifiP2P. WifiP2P (Android's implementation of Wifi-Direct) does not really fit your needs. Using WifiP2P would probably mean that a room owner creates a WifiP2P-group and any person who wants to join the room must connect to this WifiP2p-group. The problem is Android does not allow for a single device to join multiple groups at the same time (which also means it does not allow to create multiple groups at the same time). Detecting changes in available WiFiP2P devices around is also problematic (when you are already connected to a device).

如果你真的想使用 WiFi的直接您可能需要使用一个 WiFiP2P组并连接所有设备到它。该组将行为就像一个常规网络,和所有的游戏室相关的东西将必须由一个节点充当典型的服务器管理的(或也许不是典型的服务器,但一些其他网络溶液)。

If you really want to use Wifi-Direct you would probably need to use one WiFiP2P-group and connect all devices to it. This group would act just like a regular network, and all game-room-related stuff would have to be managed by one node acting as a typical server (or maybe not a typical server but some other network solution).

如果你并不真的需要 WifiP2P 只是一个普通的无线使用。所有设备连接到一个网络(甚至在设备上的一个开始热点)和整个连接过程可以是相对简单的,无需用户直接参与它在后台执行。人类可读的房间名字是不是一个问题,因为你可以从你的服务器发送任何东西。

If you don't really need WifiP2P use just a regular Wifi. Connect all devices to one network (maybe even start a hotspot on one of your devices), and whole connection process can be relatively simple, performed in background without user being directly involved in it. Human-readable room names are not a problem, since you can send anything from your server.

如果你回答是回答问题3,你真的需要使用 WifiP2P ,你需要找到某种方式来播放你的人类可读的名字到其他设备。

If you answered "Yes" to question 3. and you really need to use WifiP2P, you will need to find some way to broadcast your "human-readable" names to other devices.

您可能认为这是超级简单:您连接到其他设备,告诉你已经创建房间的名称,就大功告成了。没那么简单。正如我以前说过:

You probably think it's super easy: you connect to other device, tell the name of the room you've created, and you're done. Not that simple. As I said before:

获得 WifiP2P 连接inseperably与显示系统窗口(询问您是否真的要连接的设备)链接

obtaining a WifiP2P connection is inseperably linked with displaying a system window (asking if you really want to connect to a device)

所以整个过程会比较少:

So the whole process would be more-less:


  1. 设备答:的同修连接到设备B(这一步可以通过设备B以及执行的)

  1. Device A: initiate connecting to Device B (this step can be performed by device B as well)

设备B:的丑陋系统窗口出现,询问您是否要连接到 Android的ao12ij219sa (有些编程不变是否系统设备名称)

Device B: ugly system window appears asking you if you want to connect to Android-ao12ij219sa (some PROGRAMMATICALLY UNCHANGEABLE system device name)

设备B:的接受

设备B:的等待连接的结果

设备B:的连接,等待有关设备A的房间名称信息

Device B: connected, waiting for information about Device A's room name

设备答:的连接,发送房间的名称

Device A: connected, sending room's name

设备B:的获取设备A的房间名称,断开(未来的连接请求也将导致显示系统窗口)

Device B: Device A's room name obtained, disconnecting (future connection request will also result in showing system window)

虽然步骤1-7正在执行没有其他设备可以连接到设备A(因此在此期间没有其他设备可以获取房间的名称)。

While steps 1-7 are being performed no other device can connect to Device A (so during that time no other device can obtain room's name).

可以做些什么:


  1. 使用 WifiP2P服务发现 。它允许广播的一些信息,而无需获得的连接。可惜的是它甚至更糟docummented比 WifiP2P 本身,它可能会更不可靠(主观)。如果你想基于一个可靠的解决方案,你将需要花费测试和寻找不同的情况,即可能无法正常工作的时间。我的经验,这是可能知道,但需要从你实施了一些解决方法,因此它可以始终获得稳定的 WiFiP2P 连接。

  1. Use WifiP2P Service Discovery. It allows for broadcasting some information without the need to obtain a connection. Unfortunatelly it's even worse docummented than WifiP2P itself, and it might be even less reliable (subjectively). If you want to make a reliable solution based on that you will need to spend hours of testing and finding different cases in which it might not work. I know from experience that it's possible, but will require from you a number of workarounds to be implemented so it can always obtain a stable WiFiP2P connection.

发送到您的设备,那些(不变) WifiP2P 名称和创建房间名称之间的映射,在一些其他的方式。例如通过您的服务器的API或东西...

Send to your devices, a mapping between those (unchangeable) WifiP2P names and created room names, in some other way. For example through your server api or something...

这篇关于无线网络连接P2P。告知可用一些事件的所有同行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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