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?
Http requests should work. Why would the browser block it? If itâs a real-time game websocket connections should work.
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?
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: GitHub - britzl/defold-websocket: DEPRECATED - Use https://github.com/defold/extension-websocket instead!
I will look into your problem today.
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
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.
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: GitHub - britzl/defold-websocket: DEPRECATED - Use https://github.com/defold/extension-websocket instead!
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.
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