Jump to content
  • Advertisement
Sign in to follow this  

Chat Server- Better Way of interpreting commands?

This topic is 2611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a chat program as an introduction to network programming in python, using the asyncore module. It is all working fine, but I want to make my command system better.This is how I handle commands like login,logout,look(see other people in the same chat room),etc.:

def handle(self, session, line):
'Handle a received line from a given session'
if not line.strip(): return
# Split off the command:
parts = line.split(' ', 1)
cmd = parts[0]
try: line = parts[1].strip()
except IndexError: line = ''
# Try to find a handler:
meth = getattr(self, 'do_'+cmd, None)
# Assume it's callable:
meth(session, line)
except TypeError:
# If it isn't, respond to the unknown command:
self.unknown(session, cmd)

It works fine if you use it the right way, but I want to make it more user-friendly by changing the way it works a bit. First off, whenever you want to send a message, you have to type "say " first, while most users would be accustomed to just typing something in and expecting the client to show it. I want to prefix all the commands with a "/" so when the user types in "/login heromal" they will login with the name heromal. Everything that isn't a command will be treated as general chatting(thereby eliminating the need for a say command, since you will say everything that isn't a command). How do most chat programs go about interpreting chat commands?
Here is the full code(about 200 lines so far):http://pastebin.com/0yjk666A

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!