WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

 
 
Thread Tools Rating: Thread Rating: 23 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 05-11-2007, 03:25 PM
pattie pattie is offline
Member
 
Join Date: Aug 2006
Posts: 32
Client Server Networking problem

Sorry about posting this thread in the wrong place.

Here is the code I am using:

SERVER SERVER SERVER SERVER:

import viz
import viznet
viz.go()
viz.clearcolor(.2,.2,.4)
view = viz.get(viz.MAIN_VIEWPOINT)
view.setPosition(70,45,-60)
view.lookat(0,0,10)
objectList = ['duck.wrl', 'logo.wrl', 'male.cfg', 'female.cfg' ]
maze = viz.add('tankmaze.wrl')
objects = {}
UPDATE = viznet.id('update')
CLIENT_DISCONNECTED = viznet.id('client_disconnected')
CHAT = viznet.id('chat')
viznet.server.start(port_in=14950, port_out = 14951)
def onStartClient(e):
print '** Client joined:', e.sender
viz.callback(viznet.CLIENT_CONNECT_EVENT, onStartClient)
def onStopClient(e):
print '** Client left:', e.sender
viznet.server.send(CLIENT_DISCONNECTED, client=e.sender)
objects[e.sender].remove() #Remove object from map
del objects[e.sender] #Delete user from array
viz.callback(viznet.CLIENT_DISCONNECT_EVENT, onStopClient)
def update(e):
try:
objects[e.sender].setPosition(e.pos)
objects[e.sender].rotate(e.ori)
except KeyError:
objects[e.sender] = viz.add(objectList[e.object],scale=
2,2,2],pos=e.pos,euler=e.ori)
objects[e.sender].tooltip = e.sender
viz.callback(UPDATE, update)

import viztip
tth = viztip.ToolTip()
tth.start()
tth.setDelay(0)

def updateChat(msg):
text.message('\n'.join(text.getMessage().split('\n ')[1:])+'\n'+msg)

text = viz.addText('\n\n\n\n', viz.SCREEN) # send 4 carriage returns to screen
text.alignment(viz.TEXT_LEFT_BOTTOM)
text.fontSize(25)
text.translate(0.05,0.08)

input = viz.addText('>_', viz.SCREEN) # Add Chat prompt
input.alignment(viz.TEXT_LEFT_BOTTOM)
input.fontSize(25)
input.translate(0.032,0.05)
input.visible(0) #Only visible when in type mode

def onChat(e):
updateChat(e.client+': '+e.chat)
viz.callback(CHAT,onChat)

talking = False
def onKeyDown(key):
global talking
if key == 'y' and not talking: # Start talking mode
talking = True
input.message('> _')
input.visible(1)
elif key == viz.KEY_ESCAPE and talking: # Exit talking mode
talking = False
input.visible(0)
return True
elif talking and key == viz.KEY_RETURN: # Send message
talking = False
viznet.server.send(CHAT,client='SERVER',chat=input .getMessage()[2:-1])
input.visible(0)
# Update display with our message
updateChat('SERVER'+': '+input.getMessage()[2:-1])
elif talking and key == viz.KEY_BACKSPACE: # Delete a character
if len(input.getMessage()) > 3:
input.message(input.getMessage()[:-2]+'_')
elif len(key) > 1: # Handle special keys
return False
elif talking and 32 <= ord(key) <= 126: # Input message
input.message(input.getMessage()[:-1]+key+'_')
viz.callback(viz.KEYDOWN_EVENT,onKeyDown,priority=-6)

CLIENT CLIENT CLIENT CLIENT

import viz
import viznet
import vizinfo
viz.go()
viz.clearcolor(.2,.2,.4)
maze = viz.add('tankmaze.wrl')
objects = {}
SEND_UPDATE_TIMER = 1
UPDATE = viznet.id('update')
CLIENT_DISCONNECTED = viznet.id('client_disconnected')
CHAT = viznet.id('chat')
SERVER_NAME=viz.input('Enter Server Name')
while not viznet.client.connect(SERVER_NAME,port_in=14951,po rt_out=14950):
if viz.ask('Could not connect to ' + SERVER_NAME + ' would you like to try another?'):
SERVER_NAME=viz.input('Enter Server Name')
else:
viz.quit()
break

objectList = ['duck.wrl','logo.wrl','male.cfg','female.cfg']
obj_choice=viz.choose('Connected. Select your character',objectList)

def mytimer(num):
if num == SEND_UPDATE_TIMER:
ori= viz.get(viz.HEAD_ORI)
pos = viz.get(viz.HEAD_POS)
viznet.client.sendAll(UPDATE,client=viz.getCompute rName
(),object=obj_choice,pos=pos,ori=ori)
viz.callback(viz.TIMER_EVENT, mytimer)
viz.starttimer(SEND_UPDATE_TIMER, .05, -1)

def update(e):
if e.client != viz.getComputerName():
try:
objects[e.client].setPosition(e.pos)
objects[e.client].rotate(e.ori)
except KeyError:
objects[e.client] = viz.add(objectList[e.object],scale=[2,2,2],pos=e.pos,euler=e.ori)
objects[e.client].tooltip = e.client
viz.callback(UPDATE,update)

def client_disconnected(e):
objects[e.client].remove()
del objects[e.client]
viz.callback(CLIENT_DISCONNECTED,client_disconnect ed)

def onStopServer(e):
print 'Disconnected from server'
viz.quit()
viz.callback(viznet.SERVER_DISCONNECT_EVENT,onStop Server)

import viztip
tth = viztip.ToolTip()
tth.start()
tth.setDelay(0)

def updateChat(msg):
text.message('\n'.join(text.getMessage().split('\n ')[1:])+'\n'+msg)

text = viz.addText('\n\n\n\n',viz.SCREEN)
text.alignment(viz.TEXT_LEFT_BOTTOM)
text.fontSize(25)
text.translate(.05,.08)

input = viz.addText('> _',viz.SCREEN)
input.fontSize(25)
input.translate(.032,.05)
input.visible(0)

def onChat(e):
updateChat(e.client+': '+e.chat)
viz.callback(CHAT,onChat)

talking = False
def onKeyDown(key):
global talking
if key == 'y' and not talking:
talking = True
input.message('> _')
input.visible(1)
elif key == viz.KEY_ESCAPE and talking:
talking = False
input.visible(0)
return True
elif talking and key == viz.KEY_RETURN:
talking = False
viznet.client.sendAll(CHAT,client=viz.getComputerN ame(),chat=input.getMessage()[2:-1])
input.visible(0)
elif talking and key == viz.KEY_BACKSPACE:
if len(input.getMessage()) > 3:
input.message(input.getMessage()[:-2]+'_')
elif len(key) > 1:
return False
elif talking and 32 <= ord(key) <= 126:
input.message(input.getMessage()[:-1]+key+'_')
viz.callback(viz.KEYDOWN_EVENT,onKeyDown,priority=-6)
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:01 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC