About Monkey 2 › Forums › Monkey 2 Projects › Introducing: HTML WebSockets for MX2! + GitHub
Tagged: html, web sockets, websockets
This topic contains 8 replies, has 4 voices, and was last updated by
Richard Betson
7 months, 3 weeks ago.
-
AuthorPosts
-
December 18, 2017 at 5:35 am #12344
First things first, the repo: https://github.com/forgotten-king/Monkey2-HTML-WebSocket/tree/Dev
Hello, everyone! I’ve just finished developing a working Monkey2 port for WebSockets. This is purely a JavaScript wrap and not available for other exports. Being that this is my first C++ project and third* (2nd) real Monkey2 project this may be a bit rough around the edges. It is currently implemented as a near 1 to 1 implementation with actual HTML5 sockets minus a few smaller things IE:can only send strings, there is also a potential memory flaw with retrieving malocc’d strings from JavaScript.
For anyone interested, once the Module has been accepted it should eventually be accessible through the Modules as “html-ws” and under namespace html.ws. All documentation for this implementation will be found in the main monkey2 file, the C++ files are bare.
As it stands, this is an introductory version, whatever that means, but will be usable in projects. I personally don’t have any problems with my implementation but I figure, hope, there will be suggestions and requests for the current API. I anticipate most comments will be about requesting a near 1 to 1 implementation for the native socket API. I’m not sure how possible it will be, but I will make my best attempt if enough people request or Mark himself prefer it over my current version.
# Spoilers
video: LINK (because thumbnail ruined quality)
Code:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889Namespace ws_demo#Import "<std>"#Import "<mojo>"#Import "<html-ws>"Using std..Using mojo..Using html.ws..Class DemoSock Extends WebSocketField Messages:Deque<String>Method New()Super.New("ws://echo.websocket.org/")Messages = New Deque<String>()EndMethod Send(data:String) OverrideIf Self.ReadyState = ReadyStates.OPEN ThenSuper.Send(data)EndEndMethod AddMessage(msg:String)Messages.PushLast(msg)If Messages.Length > 15 ThenMessages.PopFirst()EndifEndProtectedMethod OnData(data:String) OverrideAddMessage( "Recieved: " + data )EndMethod OnOpen() OverrideAddMessage("Socket established connected!")EndMethod OnClose() OverrideAddMessage("Socket terminated!")EndEndClass MyWindow Extends WindowField socket:DemoSockField sends:Int = 0Method New( title:String="Websocket Demo", width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )ClearColor= Color.Bluesocket = New DemoSockEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText( "Monkey Web Sockets. Enter*connects* __ Space*sends hello* __ Esc *closes*, ",0,0 )socket.Update()Local I:= 0For Local msg:=Eachin socket.MessagesI += 1canvas.DrawText( msg, 20, 40 + I * 20 )NextEndMethod OnKeyEvent( event:KeyEvent ) OverrideIf event.Type = EventType.KeyDown ThenSelect event.KeyCase Key.Spacesocket.Send("Hello # " + sends )sends += 1Case Key.Entersocket.AddMessage("*initiating a connection*")socket.Connect()Case Key.Escapesocket.AddMessage("*attempting to close connection*")socket.Close()EndEndEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndDecember 18, 2017 at 8:24 am #12351So, what can you do with these websockets and games? Is it for multiplayer game like quake in the webbrowser and such?
December 18, 2017 at 8:39 am #12353To answer your question: Yes! If you wanted to make a multiplayer game like quake for browsers WebSockets are the way to go!
To add more details about web sockets:
The reason WebSockets are so important is that they’re currently the most widely supported way of real-time communication from the browser. I can’t explain it better than other articles out there, but for Monkey2 it means you can now have real-time (web)socket based applications in your browser games – which is actually not currently supported in Monkey2 – excluding this module.Perhaps you wanted to make an online chat room on Monkey2. It can be done multiple ways, but WebSockets were introduced because of the rising demand of real-time connected applications.
So it would look like this:
1. Create a server in a language which has some web-socket host libraries. JavaScript is a good candidate though not what I use.
2. Once your server is set up to handle web sockets, it’s time to make your client
3. All you have to do is extend the WebSocket class and define some handles and you’re essentially done!December 19, 2017 at 5:20 am #12367I’ll be checking this out.
December 29, 2017 at 3:13 am #12491Update for everyone whos been wanting to try it out! I’m not sure how long it will be until my module is excepted but here is the repository! Feel free to dig/play around! The banana should include everything you need to start out with. Feel free to ask questions here or on the issues of the repo.
Github: https://github.com/forgotten-king/Monkey2-HTML-WebSocket/tree/Dev
December 29, 2017 at 8:39 pm #12511Awesome, this is just great, thanks!
December 31, 2017 at 7:04 am #12535Hey Abe, so it should be possible to create a server application in MX2 which listens to classic sockets plus websockets? As far as I understood this stuff you just need to react in a different way, so you have to send back kind of a http header during connect?
December 31, 2017 at 6:53 pm #12537@xaron As far as I’m aware, that should be completely possible. I’ve actually done it in a different language if you’d like to see! Though, the WebSocket transformations were apart of a standard library in the language. But if you would like to learn more about making a WebSocket server that is the base idea. You can find out more here https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
I will probably be making a WebSocket transformation API sometime in the future, but it’s not a priority of mine at the time being. I can try taking a look at it sooner than later if you’d like! It wouldn’t be a part of this API though
August 26, 2018 at 11:44 pm #15332Got the demo working on Linux.
Playtime!
-
AuthorPosts
You must be logged in to reply to this topic.