Guest User

Untitled

a guest
Oct 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import sys
  2. from time import sleep
  3. from sys import stdin, exit
  4.  
  5. from PodSixNet.Connection import connection, ConnectionListener
  6.  
  7. # This example uses Python threads to manage async input from sys.stdin.
  8. # This is so that I can receive input from the console whilst running the server.
  9. # Don't ever do this - it's slow and ugly. (I'm doing it for simplicity's sake)
  10. from thread import *
  11.  
  12. class Client(ConnectionListener):
  13.     def __init__(self, host, port):
  14.         self.Connect((host, port))
  15.         print "Chat client started"
  16.         print "Ctrl-C to exit"
  17.         # get a nickname from the user before starting
  18.         print "Enter your nickname: ",
  19.         connection.Send({"action": "nickname", "nickname": stdin.readline().rstrip("\n")})
  20.         # launch our threaded input loop
  21.         t = start_new_thread(self.InputLoop, ())
  22.    
  23.     def Loop(self):
  24.         connection.Pump()
  25.         self.Pump()
  26.    
  27.     def InputLoop(self):
  28.         # horrid threaded input loop
  29.         # continually reads from stdin and sends whatever is typed to the server
  30.         while 1:
  31.             connection.Send({"action": "message", "message": stdin.readline().rstrip("\n")})
  32.    
  33.     #######################################
  34.     ### Network event/message callbacks ###
  35.     #######################################
  36.    
  37.     def Network_players(self, data):
  38.         print "*** players: " + ", ".join([p for p in data['players']])
  39.    
  40.     def Network_message(self, data):
  41.         print data['who'] + ": " + data['message']
  42.    
  43.     # built in stuff
  44.  
  45.     def Network_connected(self, data):
  46.         print "You are now connected to the server!!!"
  47.    
  48.     def Network_error(self, data):
  49.         print 'error:', data['error'][1]
  50.         connection.Close()
  51.    
  52.     def Network_disconnected(self, data):
  53.         print 'Server disconnected'
  54.         exit()
  55.  
  56. if len(sys.argv) != 2:
  57.     print "Usage:", sys.argv[0], "host:port"
  58.     print "e.g.", sys.argv[0], "localhost:31425"
  59. else:
  60.     host, port = sys.argv[1].split(":")
  61.     c = Client(host, int(port))
  62.     while 1:
  63.         c.Loop()
  64.         sleep(0.001)
Add Comment
Please, Sign In to add comment