Network test

Monkey Forums/Monkey Programming/Network test

skid(Posted 2013) [#1]
I'm slowly getting the hang of python programming on google app engine.

The following is a network chat test, select html5 preview, then click connect, then wait, then click chat.

http://nitromux.appspot.com/

I'm up for next hour if you want to say hi...

It's also embedded in a facebook page here:

http://www.facebook.com/nitrologic/app_581373191875358


slenkar(Posted 2013) [#2]
does it do the push notifications or some kind of direct connection thingie


skid(Posted 2013) [#3]
The monkey client fires off multiple httprequests per second which get answered by a python script running on an appengine account.

Once the game starts to take shape I will be adding game hosting that runs at 10 packets per second. If this works out OK I would like to start hosting other monkey games that want to go multiplayer.

The ping to nitromux.appspot.com is 170ms from here in nz so that's kind of suck for low latency competition.

The python server currently looks like this, still needs more tidy and optimizing before I add anything more.

import cgi
import os
import time

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import memcache
from google.appengine.api import users

class Session(db.Model):
	player = db.UserProperty()
	simulation = db.StringProperty()
	lastframe = db.IntegerProperty()
	startTime = db.FloatProperty()
	nextSlot = db.IntegerProperty()

def getSim():
	simcount=memcache.get('simcount',0)
	return 'sim'+str(simcount)

class ResetSync(webapp.RequestHandler):	
	def get(self):
		simcount=memcache.incr('simcount',1,None,0)
		json="[sim,"+getSim()+"]"
		self.response.out.write(json)

class ConnectSync(webapp.RequestHandler):	
	def get(self):
		sim=getSim()
		sessioncount=memcache.incr('sessions',1,None,0)
		session = Session()
		session.key='session'+str(sessioncount)
		session.startTime=time.clock()
		session.nextSlot=0
		session.simulation=sim
		session.lastframe=0
		if users.get_current_user():
			session.player = users.get_current_user()
		memcache.set(session.key,session);		
		json="[connect,"+session.key+"]"
		self.response.out.write(json)
 	
class ServeSync(webapp.RequestHandler):	
	def get(self):
		session_id = self.request.get('session')
		if session_id:
			session = memcache.get(session_id)
			if not session:
				self.response.out.write('[]')
				return
			sim=session.simulation
			t=time.clock()-session.startTime
			slot=int(t*10)
			msg=self.request.get('msg')
			if msg:
				messagecount=memcache.incr(sim+'_mcount',1,None,0)
				msgkey=sim+'msg'+str(messagecount)
				memcache.set(msgkey,'['+str(slot)+',['+msg+']]')

			lastmessagecount=session.lastframe
			messagecount=int(memcache.incr(sim+'_mcount',0,None,0))
						
			msg=""
			if messagecount>lastmessagecount:

				session.lastframe=messagecount			
				memcache.set(session.key,session)

				for i in range(lastmessagecount,messagecount):

					if i>lastmessagecount+1:
						msg+=","
						
					msgkey=sim+'msg'+str(i+1)
					mget=memcache.get(msgkey)

					if mget:
						msg+=mget
					else:
						msg+="ERR"
									
			msg='['+sim+','+str(slot)+',['+msg+"]]"

			self.response.out.write(msg)
		
class IndexPage(webapp.RequestHandler):
	def get(self):
		path = os.path.join(os.path.dirname(__file__), 'index.html')
		self.response.out.write(template.render(path, None))

application = webapp.WSGIApplication([
	('/', IndexPage),
	('/sync', ServeSync),
	('/reset', ResetSync),
	('/connect', ConnectSync)],debug=True)
									
def main():
	run_wsgi_app(application)

if __name__ == '__main__':
	main()