How to Support Multipeer Connectivity ?
Pythonista can support iOS Multipeer Connectivity?
who can show some sample code?
How do I send data from client to server and vise versa during game?
@robertiii be aware, my code on github is still unfinished. I did this some months ago to proof that MPC is really working in Pythonista . Thats what the code does well. I uploaded it then for @mikael as an working example. But it does not yet serve as an solid and simple API, a lot of polishing and refactoring is still needed here. I am still at it, but my time for it is restricted so don't expect a finished API very soon. Of course you decide, but I would wait for @mikael 's Grand Python Simplyfication !!! That is what I am also looking forward to ;-) because he has already done some great APIs for pythonista (see here https://github.com/mikaelho).
Here's a minimal usage example, a line-based chat:
import multipeer my_name = input('Name: ') mc = multipeer.MultipeerConnectivity( display_name=my_name, service_type='chat' ) try: while True: chat_message = input('Message: ') mc.send(chat_message) finally: mc.end_all()
This is a functional chat, even though the prompts and incoming messages tend to get messily mixed up.
You can also run the
multipeer.pyfile to try out a cleaner Pythonista UI version of the chat, which demonstrates sending dicts as the message content, and acts as the best 'how-to guide' at the moment.
Docs still need some significant work. All testing and comments highly welcome.
Noted that shutting and restarting the UI chat client leads to duplicate messages being received, at least with 3 peers, and general instability. I will try including the checks to not re-create classes when already available.
Also, need to implement proper error handling and exceptions if possible.
btw, those try/except to not recreate the classes would not have worked,
would fail because SessionDelegate was cleared.
You might first try
though, note that the objc class name can change when using "debug" mode.
@JonB, thanks. I would like to say that I understood completely what you said... But no, I didn’t. For clarity, I understood that the thing below would be sufficient, barring debug on/off people?
try: AdvertiserDelegate = ObjCClass('AdvertiserDelegate') ADelegate = AdvertiserDelegate.alloc().init() except: # ...
Eh, the issue was with the UI chat demo code, not the MC framework. Seems to work pretty well now, even tolerates and recovers from being backgrounded without any additional effort from the developer.
Next I will take a look at capturing and managing errors from send, mainly.
Would be good to hear if someone has a use case for the streaming or resource-send options, which are much more tricky.
create_objc_class will create a new class name every time it is run (at least, with the default debug attrib set to 1, ) -- so
'AdvertiserDelegate'might really become
Only really an issue for development, but if you see strange things where a change you just made doesn't seem to be working, thats the reason.
@mikael very super 👍🏿 cool indeed. I do really love your approach with this simple API. Just tried it and it works excellent. Will have a look at the source later...
Edit: made a minor bugfix pull request to avoid an exception of being thrown
Great work! Its incredible! Why is it limited to only 8 devices?
@robertiii, seems to be an Apple-defined limit, that may be driven by Bluetooth LE limits. It is now hard to find concrete current information in Apple docs, but I wanted to make that limit visible so that you get warned before embarking on an ambitious project targeting hundreds of peers in one session.