Can I make this very simple multiplayer game with websocket?

Hi!

I’d like to create a quiz game where players use their mobile phones to respond (think Kahoot! or quiplash, but very simple). Basically it’s a true/false or pub quiz type thing (p.s it’s actually Malas Decisiones, but you know what I mean). It would work like this:

  1. On a desktop, you’d start the game and a 5 digit room code would be generated.

  2. Players would go to a URL on their device and enter their room code and nickname.

  3. Once all the players are connected, the host clicks “start” on the desktop and the first question appears on the screen. Players either press “true” or “false” on their phone.

  4. Once every player has answered, the correct answer is revealed, points are rewarded, and we move on to the next question.

Is this possible with just DefoldWebSocket? Or would it require Nakama or another plug in like @lerg’s multiplayer tag websocket projects? I am starting from 0 in terms of the networking here so just looking to get some ideas of what tools are necessary (@Alex_8BitSkull was looking at Nakama but it seems WAY too complicated for what we need…)

1 Like

If you want just a clean TCP connection, you could use LUA socket. I have written this script and works well with a server software in same machine. Perhaps it should need adjustment or could even not work in “real” internet conditions, with lag an lost of packets. Note that the tcp_client and tcpclient_state are both global variables and the functions should be in a separate LUA module.

tcpclient = socket.tcp()
tcpclient_state = -1

function net_connect_to_server()
	tcpclient_state = tcpclient:connect("127.0.0.1", 8888)
	tcpclient:settimeout(30)
	repeat
	until tcpclient_state ~= -1
	if tcpclient_state ~= 1 then
		print("server down")
	elseif	tcpclient_state == 1 then 
		print("connected")
		tcpclient:settimeout(0.010)
	end
end

function net_get_data()
	-- implementar controle de erros em caso de desconexão
	local servermsg = tcpclient:receive()
	if servermsg ~= nil then
		return servermsg
	end
end

function net_send_data(adata)
	local result = tcpclient:send(adata)
end

Once connected, just put the call into the update function to get any data server has sent:

function update(self, dt) 

	local server_data = net_get_data()

If you use this code or part of it, please tell us if it worked for you. Good luck.

2 Likes

hi @lightenlynx!

Please forgive my ignorance: I believe that LuaSocket is a plug-in. It seems to be available at http://w3.impa.br/~diego/software/luasocket/installation.html. However, I don’t know how to include it in my project, how to install it, or how to make it interact with defold. Could you possibly tell me how to get that working?

Many thanks

edit: just to be clear, i’m getting 404 from https://www.cs.princeton.edu/~diego/professional/luasocket

edit II: found it on github!! NEVER MIND!

1 Like

I don’t really remember if I installed as a plugin or if it came with Defold (even when I start a new project, the socket is available) but I am glad you found it already.

(Just in case anyone from the future reads this, it’s at https://defold.com/assets/luasocket/)

3 Likes

You want: LuaSocket

perfect!

Just a quick question: is lua socket the most appropriate tool for the game i am making?

edit: Okay, i have got websocket sending and receiving! this is great progress. Now, how do i set up a server?

Edit 2: okay, to be specific, I need to learn how to get my own URL for message passing via ws. Any pointers? Thanks for your help so far.

1 Like

I have no experience with WebSockets so I can’t tell you about that. I am more like an old-school programmer. But to set up a server, you gotta have a “server software” running with an open port to listen to connection requests. It would help if you learn about TCP client/server stuff, but in a nutshell it would be like that:

If you want to develop, same machine (ie: localhost, 127.0.0.1) or same network should be ok.

For the server software itself, you can build it on almost any programming language. You have to bear in mind the type of operating system you will run your server (windows? linux?) and then chose the most suitable. I recomend you to look up NodeJS to build your server.

Well, that’s it, you got to do a little reading and testing so you can grab the client-server concept.

2 Likes

If you’d like to stick with Lua for a server, I’d recommend Luvit. The API docs aren’t great, but most of what you need to know about the libraries can be found in their examples.

1 Like

Never heard of it, I will take a look. Thanks.

1 Like

Lightenlynx: thank you. Yes, i have a lot to learn. It’s just great to have a few people say that i am at least on the right track and i am learning the right thing. Thanks also to JustAPotota. I am looking into this!!

3 Likes