What kind of connection for html5 online game?

Hi friends
usuall there are some limitations on http requests and socket connections in html5 builds, browsers block http request, some problem using websockets too and etc.
my last project was online game using unity and node js and http and websocket for online stuff, both of them are ok on android and windows build but not on html5.
so whats your suggestion to a person like me who wants to make online html5 game using defold? what kind of connection or protocol should i use that works well on html5?

1 Like

Http requests should work. Why would the browser block it? If it’s a real-time game websocket connections should work.

2 Likes

it’s kind of error on sending http request from browser:
Access to XMLHttpRequest at ‘url’ from origin ‘http://localhost:7203’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
no different which engine it is, i get this error on html5 outputs in unity and defold and gamemaker,… but the same http request to the same url works on windows and android ( browser makes this error )
last night I have tested defold websocket, it works on windows bundle well but on chrome browswer when i try to send and receive data it gives this error:
DEBUG:SCRIPT: Connected
dmloader.js:346 Assertion failed: undefined …

No idea why. Can you package this into an example project so that I can try as well?

1 Like

Hi britzl
I have sent a link for you but some new results:
i used DefNet dependencies to make tcp connection to my server too, i changed the ip and port and start to sending data from defold to server, it works well in desktop budle and no problem. i changed it to build html5 and i got this error on browser console:
TCP_wasm.js:1 WebSocket connection to ‘ws://171.2*.**.1*1:4000/’ failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE

and these messages on server side too:
new client
Received: GET / HTTP/1.1
Host: 171.2*.**.1*1:4000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
Upgrade: websocket
Origin: http://localhost:12439
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Sec-WebSocket-Key: MhxAimdjGTkc0rVMchTvgw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: binary
client left

it seems it tries to chage tcp to websocket but something goes wrong. I’m not sure if it gives you any usefull info but just some result…
sorry i wasted lot of time of you by my online problems, thats one of important part i need with game engines…

This is expected. There is no such thing as s TCP socket in HTML5. You must use web sockets: https://github.com/britzl/defold-websocket

I will look into your problem today.

1 Like

Thanks britzl, as i said different problems about online stuff maybe you get confused, my online problems are:

  • on windows build tcp works well but http request to my http server doesn’t work ( I have sent my server ip and port to you, maybe you could find problem if you send a request using your code )
  • on http build websocket doesn’t work while it works on windows build
    I know you are busy with lot of stuffs, no problem if we solve it late, I still should learn lot of things on defold and have long way on it, but i want to make sure i’ll not have problem in online part later :smiley:
    Thanks for your support

I will test this.

Are you using defold-websocket?

Yes I got it’s dependencies and i used them, it works on windows version. I have sent a link to your message which you can download zip version of my sample project. is there any ready to use websocket project for html5 that i can test on my system?
i use ws not wss and for http i use http not https

I can connect to your server on macOS:

{ --[[0x113c7cd20]]
  status = 304,
  response = "I got new request",
  headers = { --[[0x113c7ce10]]
    x-powered-by = "Express",
    connection = "keep-alive",
    date = "Wed, 03 Jun 2020 06:26:15 GMT",
    etag = "W/"11-omrcD+NenePYy7jEB4MOvKpdkc4""
  }
}

On HTML5 I get the “CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource”. You can solve this by adding a response header to incoming requests:

So the question remain with Windows. I’m guessing firewall or antivirus preventing dmengine from connecting. Can you make a HTTP request anywhere or is it always blocked. Try this:

http.request("http://www.defold.com", "GET", function(self, id, response)
	pprint(response)
end)

The defold-websocket project can be opened as a normal Defold project and there’s an example you can try.

You can also try Colyseus or Nakama. Both use Defold Websockets to connect to their servers.

2 Likes
http.request("http://www.defold.com", "GET", function(self, id, response)
	pprint(response)
end)

it doesn’t work, same error:

ERROR:SCRIPT: Unable to create HTTP connection to ‘http://www.defold.com’. No route to host?
WARNING:DLIB: Profiler Message.Dispatch @__http_worker_0 took 60.092 seconds
DEBUG:SCRIPT:
{ --[[000001ECCDE737A0]]
status = 0,
response = “”,
headers = { } --[[000001ECCDE65270]]
}

i can make html request anywhere using other tools or game engines, sending request to “https://www.google.com/” using defold http.request gives response too but sending to my server and “http://www.defold.com” not work!

=======
I have tested your example websocket project too, again it works on windows, it works on microsoft edge but not in chrome, using chrome gives this error:
WebSocket connection to ‘ws://echo.websocket.org/’ failed: Error during WebSocket handshake: Sent non-empty ‘Sec-WebSocket-Protocol’ header but no response was received
createPeer @ DefoldWebsocket_wasm.js:1

It could be that dmengine (ie the engine that is launched when you do Project->Build) is blocked by your system somehow. Do you have a firewall? Antivirus?

@pkeod do you have problems with http requests on Windows?

Ok, so this works?

http.request("https://www.google.com", "GET", function(self, id, response)
	pprint(response)
end)

And what if you try https://www.defold.com (note https!)

This is explained here: https://github.com/britzl/defold-websocket#important-note-on-sec-websocket-protocol-and-chrome

I tried on Windows 10 using Parallels and I was able to connect:

{ --[[000001A8A346A940]]
  status = 301,
  response = "",
  headers = { --[[000001A8A346AA30]]
    server = "AmazonS3",
    connection = "keep-alive",
    content-length = "0",
    x-cache = "Hit from cloudfront",
    x-amz-cf-pop = "ARN1-C1",
    via = "1.1 d2344bac503cfadf51884e9930680ffe.cloudfront.net (CloudFront)",
    location = "https://defold.com/",
    age = "5188",
    x-amz-cf-id = "siA27jPwgE3nLYDWibY_Ago5_0sYBoldRknNwtzNlN0jD6MVIyLzKg==",
    date = "Wed, 03 Jun 2020 06:31:42 GMT"
  }
}

(Redirect to https)

I did get a Windows Firewall popup:

I don’t have any extra antivirus software, I’ll check for firewall or something that may block it but i don’t think it’s because of firewall, usually windows gives alert or some question in these cases.
I made executable version to check maybe it’s because of defold Project-> Build but the executable version doesn’t gives response too ( exe file )

not with other programs, i have problem with defold http requests on windows, but it seems https request work, for example this one works well:

http.request("https://www.google.com", "GET", function(self, id, response)
	pprint(response)
end)

the code above gives correct response.

can i do something about it? should i change something to make it work with chrome?

Yes, make sure the server you connect to replies with the same “Sec-WebSocket-Protocol”. I assume this is handled by WebSocket game servers such as Colyseus? @endel might be able to answer.

1 Like

windows gave me this popup when i wanted to use websocket and i pressed allow, but it doesn’t show any popup for http. i added defold itself to my firewall list to give permission to it but still have problem using http.request. maybe I should add dmengine itself. let me see

Yes, it is dmengine that makes the http request, not Defold.

There are more than one folder which has dmengine.exe on it! i have to add permission for all of them?!
why windows doesn’t give me popup itself about it? it gives me for websocket use and i simple press allow and it works

I have three dmengine allowed on my firewall list but still http request doesn’t work. i think firewall is not the reason for this, specially because https works but http not works

– I have removed all of them and i pressed Project-> Build again. windows show pupop and i pressed allow, but still http doesn’t work

I have tested http request in android build to my server, it works on Android version but that’s not good too. it sends the request about 10 seconds after i call http.request! ( i see the request receive message on server about 10 seconds after i press button to send request on Android)

Defold android build is perfect, not much settings about sdk and ndk … and very light, just 1.86MB. it’s perfect :grinning::+1: