MNet - a simple networking module
Monkey Forums/User Modules/MNet - a simple networking module
| ||
Hi all, I've started a small networking lib called MNet (Monkey Net): http://code.google.com/p/mnet/ At the moment it supports only HTTP GET requests on Android so almost nothing but this will change over time. First I'd like to get this http stuff running on all platforms... |
| ||
You could include indiepaths code, that does http requests on flash and html5 |
| ||
I'd do but the download link is dead. |
| ||
I just added HTML5 as a target. At the moment I use blocking (synchronous) calls only. This will change, especially as XMLHttpRequest in HTML5 doesn't seem to support timeouts... |
| ||
Trying your example but the I am getting an error during the Android's compile process: Specifically Any ideas? Also The HTML5 compiles without a hitch, but when I execute it the output is "result: undefined" I don't believe that is the intended result. |
| ||
Holy cow, now that was stupid. Had an old file for checkin. Will be fixed in a few minutes - at least for Android. Regarding HTML5: Yeah, I just noticed this as well. Strange enough it works sometimes then not... Will check that, thanks! Ok, XMLHttpRequest() seem to work perfectly with IE9, but not with Firefox 4.x... |
| ||
Ok, please download it again. Android should work now. |
| ||
Yeah, I am using Firefox. Will try with IE. So here we go, cross your fingers & toes: downloading... installing... building for Android... running App... ::waiting:: It Works !! <!doctype html><html><head><meta http-equiv="X-UA-Compatible" content="IE=9"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><script>window.google={ ...yada..yada.yada And just to verify, Firefox buggers out and with Explorer everything is fine! |
| ||
Wohoo! Well this HTML5 XMLHttpRequest is still very young. There might be better ways to do that. What really bothers me is that IE9 seems to be the only one being able to do it. Neither Firefox 4 nor Google Chrome seem to work with that yet... Anyway, will see that I add this for iPhone and GLFW next... |
| ||
Looks an interesting project! I have been trying to figure out how to add online high scores to my game so this is just what I have been looking for. |
| ||
I just added the iOS target. :) Could only test with the simulator so it would be nice if someone could try this on a real device please? |
| ||
Does anyone have any example code on how to actually get this code work with an online score system? |
| ||
you have to write php scripts to put the scores into the database of your website (i recommend haxe to write php scripts). So you go into the control panel of your website, create database, write the php scripts to accept numbers from a httprequest and insert them into the database, then you write the httprequest from monkey and it should work. |
| ||
I will post some examples when I find some time. |
| ||
Right will post an example tonight, have the code ready. |
| ||
And here is the online highscore list example: http://www.monkeycoder.co.nz/Community/posts.php?topic=812 |
| ||
<blockquote>Ok, XMLHttpRequest() seem to work perfectly with IE9, but not with Firefox 4.x...</blockquote>Also note that for non-ASCII characters many browsers expect the result to be in utf-8 (regardless of http headers or document declaration). |
| ||
Does mnet not work with chrome right now? I'm trying the sample and it's returning "Undefined" for "www.google.com." |
| ||
Something's wrong with this board's time tracking. I did not post the previous message 40 minutes ago!!! (which is what it's telling me right now). WTF?!?! |
| ||
Something's wrong with this board's time tracking. I did not post the previous message 40 minutes ago!!! (which is what it's telling me right now). WTF?!?! Every month at the start of the month, all the post times go whack. You'll get used to it. ;) |
| ||
BUMP! mnet doesn't seem to work in Chrome. Is that right? I'm trying the sample and it's returning "Undefined" for "www.google.com." |
| ||
Google Chrome seems to have some issues with sending requests outside the domain of the website it's in. It might be a built in security feature to prevent an XSS attack. Requests from the website it's in work just fine. So if you want cross-domain requests, you'll need to do it from the server side in the php script. --- Also, since I haven't seen MNet updated with a POST request for HTML5. I added it myself. If you want it, you can add this. mnet.html5.js mnet.monkey |
| ||
Thanks Goodlookinguy! appreciated ;) |
| ||
Trying here to get the httpget example running on a Motorola Milestone Android. It crashes on the line: 'result = mnet.HttpGet( "http://www.google.com", 3000 )' Any ideas? |
| ||
Have you set the permissions in the xml template to give the app internet access? |
| ||
No. Thanks :-) |
| ||
Hey, I've been watching this on Google Code for a while and I was wondering if you had any perceivable plans in mind to implement TCP/UDP based connections? (For at least GLFW or XNA to start) I mainly ask this because I'm no expert with networking by the use of HTTP requests. (Anything beyond using this for leader-boards just isn't within my knowledge base) I'd like to somehow implement communication with a server built in monkey for a specific target in mind. The reason for the server being programmed in monkey is so it could share many of the same elements of the game, effectively emulating the gameplay to make the implementation of latency correction and anti-cheating measures easy and precise. As for the more exotic platforms: I believe most massively multiplayer flash games communicate with an XML socket server to operate, and many HTML5 applications communicate with an XMPP server to achieve better results than from simply communicating with a typical web server. Kind regards, - Brandon. |
| ||
I have plans to do this, yes. Will take some time as I have other things to do. ;) Any help is appreciated! |
| ||
This seems an amazing functionality from my honest opinion, as right now communication with web is a great lack of monkey, and just having a look around, a bunch of games nowadays needs these kind of features. Unfortunately my knowledge is not as deep as I would like, be sure I would help you if I could. By the way, you've my moral support in any case, I know is not much but be sure I will be a happy user of whatever the improvements you make on mnet. best, |
| ||
Thanks! :) |
| ||
Hi Xaron, any update of Mnet to make us happy?? ;) best! |
| ||
Thank you Xaron ! My online highscores and registration system work perfectely with Chrome (based on your example). Any plan to make it for glfw target ? (or anyone else ?) |
| ||
Yep yep, this will still take some time as I have other stuff to do, but yes, my plans are to make it available for all targets and even real communication stuff (sockets). |
| ||
Alright, I've started to work to integrate real sockets. Will take some time but this will be useful to have some real TCP (and UDP) communication protocol. |
| ||
That sounds great, Xaron; can't wait to test it out! |
| ||
Odd...I just ran the example and got results undefined. Did something change? |
| ||
I had this problem for a while with mnet, moofoo & other until I realised the following: you have to ensure the called script is on the same server as the Monkey program that's calling it. It's a security issue (denying cross-site access). Try uploading the Monkey script & your PHP (or similar) to your server & try it. |
| ||
I just uploaded it, still getting the Undefined : |
| ||
I can't remember whether I overcame this or not using mnet - I'm using moofoo these days, and it works in the way I mentioned. I guess I must have hence the reason I'm using moofoo instead. |
| ||
What's moofoo? |
| ||
Just do a search here for it & you'll find the links to it. It's another network lib you can use. |
| ||
I never got HTML5 to work with Networking beside with IE9. Don't know what's wrong there. I still research the socket stuff for now. :) |
| ||
Good news! I'm near to finish the Pubnub integration for Android (iOS and probably WP7 will follow)! HTML5 and Flash is already done by Skn3, thank you for that and thanks for pointing me to Pubnub! Using Pubnub almost everything will be possible including real time games with peer to peer, client/server, whatever you can imagine. :) More to come! Cheers - Martin |
| ||
potentially you could send the players each others IP address using PubNub and then just send socket messages |
| ||
Oh just to avoid any headaches in the future, the flash pubnub module that is available officially on their website didn't include the channel history protocol so if you try updating the lib at some point make sure to copy the code modificationss over. I reckon it could be quite a stright forward little project to write a pubnub server emulator (eg to intercept remote pubnub calls) in nodejs or even blitzmax. I did start pondering making one before I slapped myself into sense and got on with paid work.. haha! |
| ||
Thanks Skn3. I've successfully integrated Android and will update Mnet soon. |
| ||
brilliant, any plans for ios :) ? |
| ||
Sure, iOS will be next followed by WP7. :) |
| ||
Ok, a new version is out! I've added Pubnub to Android and HTML5 which works together hand in hand. A very basic and simple example is attached as well! Please note that you can only use one channel at the moment but you can switch between several channels by subscribing to a new one (That's due to the blocking nature of Subscribe for Android). Thanks Skn3 again for pointing me on this and providing me with the HTML5 version! I hope you don't mind that I use your keys in the examples? |
| ||
Hey Xaron, good work! I have no experience with the android side of things, but surprised that the subscribe is blocking! Could they be placed in threads and managed by the glue? https://github.com/pubnub/pubnub-api/blob/master/android/src/com/aimx/androidpubnub/PushService.java#L90 I don't mind if you are using my keys, but it might be better to create some ones specific to mnet for going forward with the module :) |
| ||
Ok, I'll create my own set of keys. :) Yes, Subscribe is blocking for Java and I already placed it into its own thread so the call from Monkey is non-blocking of course. Sure it will be possible to have several channels open at the same time but I haven't implemented that yet but will do that later. edit: Ok, I've replaced your keys! |
| ||
...adding sockets (TCP) for GLFW (Windows only) and Android right now, followed by iOS. Shouldn't take too long. |
| ||
Your a machine! :D keep it up! |
| ||
LOL, I know. Ask my wife. ;) GLFW sockets are working now (both TCP server and client). :) Now to Android. |
| ||
That is solid Xaron, you rule :) I attempted to make a GIT clone of the Google Code Repository, to see if I could help adding and improving, but I get an error the repository does not exist. I'm new to GIT so maybe I'm doing something wrong. I clicked the "Source" Tab in GoogleCode and then pasted that Git url into the Clone Adress in my client. |
| ||
LOL, I know. Ask my wife. ;) Haha filth! |
| ||
In Reply to Myself Above. I was using GIT, but the repository was using HG. Conclusion: They are not the same. Problem Solved. |
| ||
Hehe, yes. I like HG more than GIT even though both are quite good. |
| ||
Ok guys, I just uploaded the sockets part for GLFW including an example. At the moment TCP sockets only work for GLFW, I'll do the Android part next. I've added a very simple example. So be sure to first create the server by setting the following line in the sockets.monkey example: Global isServer:Bool = True Run that piece then compile another sockets.monkey as client (isServer = False). Run as many clients as you want. In the example you can simply enter a text both on server and client. Please let me know if there are any bugs (I'm sure there are. ;) ) |
| ||
Alright, Android client sockets are almost working now. I think I will leave it to the client component for Android as server sockets doesn't seem to make sense for me for Android, or what do you think? For now I thought about having a stdcpp or GLFW server running where all mobile devices can connect to. |
| ||
Add server sockets for Android too. Think about peer to peer game play. One hosts and the other joins. |
| ||
Xaron would it make sense if one wants to host a game over the local WiFi net with friends nearby in addition to using bluethooth? Client sockets should be priority anyhow :) |
| ||
Yes, Bluetooth is planned too! So many things... :) |
| ||
While bluetooth is cool, i still have to see a device, where it doesn't suck the battery badly. |
| ||
Xaron, I'm having an issue compiling for iOS - getting some errors "Mnet is not polymorphic". I am just trying to use the HTTP stuff. Advice? |
| ||
Is there even an iOS port yet? |
| ||
Since 1.03 I think. Xaron notes in a post above that he hasn't tried it on a device yet though. I should clarify that Monkey compiles fine but XCode produces the not polymorphic error. |
| ||
Ah right. Sorry for that. Must had happened during my refactoring. Will fix that. I plan to add Pubnub iOS as well. |
| ||
Cool, thanks Xaron. Pubnub looks interesting. |
| ||
Yep, Pubnub is awesome. They cover a lot of problems you usually have with networking. Should be ok for "normal" usage like for turn based games. For action games it might become a bit pricey though... |
| ||
Things slow down a bit at the moment as my son got a brother. :) |
| ||
Congrats! |
| ||
Congrats:) All good stuff here:D |
| ||
Thanks guys. :) |
| ||
Hi Xaron, can you give me a help ? I'm trying to add pubnub in my app using your module, when I run your example program, all things working nice, but when I try put the code in my app I'm give this error: Monkey runtime error: ReferenceError: Can't find variable: PUBNUB . . ...monkeyPro58/modules/mojo/app.monkey<53> you have a idea of what I'm made wrong ? thank's |
| ||
Hi Xaron! Great work on mnet. It works like a charm on HTML5. Unfortunately I'm running into a bug with android. These are the details: - I'm using mnet-v1.2.0. - I've done a debug build of the pubnub example. - I click on Subscribe to channel 1. - Then I click on Subscribe to channel 2. This produces a Monkey runtime error: Unknown runtime error. It points at pubnub.monkey line 80, which reads: [monkeycode]pn.Subscribe( CHANNEL_2 )[/monkeycode] Not being a java programmer myself, I don't really understand what's happening. Any help would be much appreciated! |
| ||
@Rus: I'll check this! @Lugato: Have you tried HTML5? |
| ||
Xaron, I´m using html to test the app, the error occour if I try run the projetct in the monkey IDE. If you need I can send to you the code .. thanks |
| ||
Thanks Xaron! Would be great to get pubnub working smoothly for my app. Cheers! |
| ||
To get the httpget to work on Android, do not forget to set the permission for the app to use the internets: a) After your first compile, in your build folder you should see a templates folder, alter the AndroidManifest.xml file in there. (thx therevills http://goo.gl/V0jvj ) b) Add these lines: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> (thx therevills again http://goo.gl/kUHSA ) |
| ||
How goes the socket aspects of mnet? Btw congratulations on your new son! |
| ||
Can anyone get Mnet to work on platforms like GLFW? Or is this not supported yet? |
| ||
I think it already has sockets and pubnub on glfw. |
| ||
I get an error on the glfw socket demo. I hope the socket support extends to the other targets, it would be awesome! |
| ||
It will! Takes some time though. I will add Pubnub for iOS and WP7 first. |
| ||
I'm trying to use mnet on android, and it's returning a Null object when I use the following code: [monkeycode] Method GetGameID:Int(gname:String) Local gid:Int = -1 Local resultString:String Local mess:String = scoreURL+"?action=GETGAMEID&gameName="+gname resultString = ht.Get( mess ) ' <-- here's the problem If resultString.Length() > 0 Then Local rt$[] = resultString.Split(";") If Int(rt[0]) > -1 Then gid = Int(rt[1]) Endif Endif Return gid End [/monkeycode] Apparently, Get() is returning a Null object (for whatever reason). The Java source for Get() looks like this: public String Get( String url, int timeoutConnection, int timeoutSocket ) { HttpURLConnection con = null; InputStream is = null; try { URL connectToURL = new URL( url ); con = ( HttpURLConnection )connectToURL.openConnection(); con.setReadTimeout( timeoutSocket ); con.setConnectTimeout( timeoutConnection ); con.setRequestMethod( "GET" ); con.setDoInput( true ); // Start the query con.connect(); is = con.getInputStream(); } catch( IOException e ) { _result = "ERR_HttpGet " + e.getMessage(); } return convertStreamToString( is ); } Do I change my monkey code so it handles the Null object being returned (and how?), or do I modify the Java source so it returns an empty string if an error occurs (and how)? Thanks! :-) |
| ||
Did anybody get the socket example running yet? As already mentioned there are some methods missing or named incorrectly. Even after renaming "ReceiveFromClient()" to "Receive()" (this method does exist) it does not work. I tried for several hours now and cant establish a simple connection between server and client. |
| ||
You mean that GLFW sockets example? That should work. |
| ||
Ahh, problem solved, it was my bad! I tried to receive messages by using Receive() even if no client was connected to the server. I thought it would just return an empty string then but the server refused incoming connections then. |
| ||
Show me some love? :-) |
| ||
I dont get it... Client can now connect to the server but when it sends a message to server the server closes the connection to the client. (GetNumberOfClients() returns 0 after receiving a message) |
| ||
I keep getting the polymorphic error in IOS. Is there a certain version that does not get that error? Is there a certain version of IOS xcode I need to be using? |
| ||
Hi Xaron, could you please try this example and tell me what i'm doing wrong here? Its just a simple server and client and the client shall send a message if space is being pressed. Server: Import mnet Import mojo Function Main() New Program End Class Program extends App Field server:Socket Field messages:List<String> = new List<String>() Method OnCreate() SetUpdateRate(60) server = New Socket() server.Init() server.CreateServer(644) End Method OnUpdate() server.UpdateServer() Local m:String = server.Receive() if m <> "" Then messages.AddLast(m) if KeyDown(KEY_ESCAPE) server.Close() Error("") End End Method OnRender() Cls() SetColor(255, 255, 255) DrawText("server", 10, 10) DrawText("clients: " + server.GetNumberOfClients(), 10, 30) DrawText("client: " + server.GetLastClient(), 10, 45) Local y = 65 DrawText("Messages:", 10, y) For Local m:String = eachin messages y += 20 DrawText(">" + m + "<", 10, y) Next End End Client: Import mojo Import mnet Function Main() New Program End Class Program extends App Field connection:Socket Method OnCreate() SetUpdateRate(60) connection = New Socket() connection.Init() connection.ConnectToServer("localhost", 644) End Method OnUpdate() if KeyHit(KEY_SPACE) connection.SendBroadcast("broadcast") connection.Send("send") End if KeyDown(KEY_ESCAPE) connection.Close() Error("") End End Method OnRender() Cls() SetColor(255, 255, 255) DrawText("client", 10, 10) End End When space is pressed on clientside, the server loses the connection to client! Could you please have a look at that? Thank you |
| ||
Is socket implemented for android? |
| ||
Sockets for Android and iOS are on my list, at least the client side for now. GLFW sockets are done. I already have the Android sockets running but it's still buggy, so sit tight! ;) @tOmzone: Sorry for the late answer will look into this. @Dan: Oops, looks broken. Will fix that. Unfortunately I'm a bit limited with my time at the moment due to my litte son. ;) But over the time it will become a powerful (and of course free) lib! |
| ||
Can't wait! Thanks Xaron! |
| ||
keep up the good work when your good and ready! |
| ||
Is this still under developement ? I tried your module but it actually crash on the method ReceiveFromClient() from the socket file. |
| ||
Does it? Damnit. Well I think I make a huge rewrite of all this stuff. Monkey can do TCP stuff out of the box now so I think I'll concentrate more on stuff like Bluetooth. |
| ||
I'm doing a lot of experiment now with Mnet on Android and Html5. Everything is working fine, but if the connection fails during a game it is difficult to handle this, because MNET return a "undefined" String. Every attempt to handle this in monkey ends in a crash. It is not possible to test... [monkeycode]Result$=Http.Get( Adress, 3000 ) If Result=NULL[/monkeycode] [monkeycode]Result$=Http.Get( Adress, 3000 ) If Result[/monkeycode] [monkeycode]Result$=Http.Get( Adress, 3000 ) If Result[..3]="abc" ' to dected it was ok...[/monkeycode] but this works: [monkeycode]Result$=Http.Get( Adress, 3000 ) Print Result[/monkeycode] you will see the word "undefined" in the Console I would like to know how to handle "undefined" strings. Why is there no test against "undefined", but the console knows this state? As a workaround I had to change some code in the JAVA and JS native code: android.java: .... public String convertStreamToString( InputStream is ) { // this line is new, java seems to handle undefined as NULL: if (is == null) return "ERROR no Connection"; StringBuilder sb = new StringBuilder(); try { BufferedReader reader = new BufferedReader( new InputStreamReader( is ) ); String line = null; while( ( line = reader.readLine() ) != null ) .... html5.js: MNet_Http.prototype.Get = function( url, timeoutConnection, timeoutSocket ) { try { var client = new XMLHttpRequest(); client.open( "GET", url, false ); client.send( null ); _httpResponse = client.responseText; return _httpResponse; } catch( e ) { // this line is new. Author forgot to return something, when a error occurs: return "ERROR no Connection"; } } with this you can Check in Monkey [monkeycode]Result$=Http.Get( Adress, 3000 ) If Result[..3]="ERR" ' to dected it occured an error...[/monkeycode] |
| ||
MNET could send back an empty string. I am not at my dev system, but i had modified MNET exactly to do this because it was returning undefined strings. |
| ||
GLFW target: Under xcode 4.2.1 I get these kind of errors: main.cpp:3642: error: ‘SOCKET’ does not name a type main.cpp:3644: error: ‘SOCKADDR_IN’ does not name a type etc in Stackoverflow they said that these are only defined for Windows? I don't know enough to fix this; any ideas? typedef int SOCKET; works but there are a LOT of others :) |
| ||
Am I the only one that is getting a virus alert whenever I visit this thread? |
| ||
No virus alert here, using Chrome and AVG. |
| ||
No virus! I decided to sidestep the issue with mnet by using mnet only for html5 target and brl for the rest. Bit messy though :) |
| ||
no virus here - im using ESET too /StuC |
| ||
Read: http://www.wilderssecurity.com/showthread.php?t=313795 Maybea false positive. |
| ||
Can somebody please check or confirm, that Mnet is still working on Android 4.0, 4.1, 4.2 devices? I have massive problems with my app and I guess that MNET causes the problems. Here is a link to the description of the problem: http://www.monkeycoder.co.nz/Community/post.php?topic=4707&post=53622 *** EDIT *** It is MNET! The synchron mode of calling http requests is no longer allowed on android 4. The app ends without error message or comment. A possible solution is the async HttpRequest() mode Mark offers since Monkey V69. |
| ||
Also getting a Malware detection in Chrome on this page. It's about gameoverdose.net serving malware. |
| ||
Me too, that's pretty strange. How's that? Regarding MNet: At the moment I don't work on this as Mark has introduced TCP streaming to Monkey. |
| ||
@Moderators: Neuros picture in post #38 is triggering Malware detection in chrome. can you remove it? "http://www.gameoverdose.net/project/html5/undefined.PNG" |