View Single Post
  #11  
Old 05-11-2007, 03:25 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

I just tested your script here, running one server with two clients and it worked fine. You said that server to client communication does not work. Have you tried a simple viznet example that does not use sendAll, to make sure that this is not a problem with your network setup?

I've included a very basic viznet example. In this example, whenever a client presses the spacebar their background color should change. Also, the server will print out a message every time a client connects/disconnects. If this does not work then there might be a problem with your network setup.

SERVER SERVER SERVER SERVER
Code:
import viz
viz.go()

import viznet

#Create custom event IDs
CLIENT_KEYDOWN = viznet.id('ClientKeyDown')
SET_CLEAR_COLOR = viznet.id('SetClearColor')

#Start server
viznet.server.start()

colors = [viz.RED,viz.BLUE,viz.BLACK,viz.ORANGE,viz.YELLOW,viz.GREEN,viz.GRAY]
curColor = 0

#When client connects, send them current color
def onStartClient(e):
	viznet.server.sendClient(e.sender,SET_CLEAR_COLOR,color=colors[curColor])
	print '** Client joined:',e.sender
viz.callback(viznet.CLIENT_CONNECT_EVENT,onStartClient)

#Print out when client leaves
def onStopClient(e):
	print '** Client left:',e.sender
viz.callback(viznet.CLIENT_DISCONNECT_EVENT,onStopClient)

#When client presses space key, change color and notify all clients
def onKeydownClient(e):
	global curColor
	if e.keydown == ' ':
		curColor = (curColor + 1) % len(colors)
		viznet.server.send(SET_CLEAR_COLOR,color=colors[curColor])
viz.callback(CLIENT_KEYDOWN,onKeydownClient)
CLIENT CLIENT CLIENT CLIENT
Code:
import viz
viz.go()

import viznet

#Create custom event IDs
CLIENT_KEYDOWN = viznet.id('ClientKeyDown')
SET_CLEAR_COLOR = viznet.id('SetClearColor')

#Connect to server
viznet.client.connect(viz.input('Enter Server Name'))

#Send message to server when space key is pressed
vizact.onkeydown(' ',viznet.client.send,CLIENT_KEYDOWN,keydown=' ')

#Change color on server event
def onClearColorClient(e):
	viz.clearcolor(e.color)
viz.callback(SET_CLEAR_COLOR,onClearColorClient)

#Print out message if disconnected from server
def onStopClient(e):
	print '** Disconnected from server'
viz.callback(viznet.SERVER_DISCONNECT_EVENT,onStopClient)
Reply With Quote