Here I will explain how to communicate with the server using the Socket class. The socket class allows Asynchronous communication between the client and server. This application will show how to share data in real-time in two different browser windows using Silverlight.
This application will contain two parts:
- Server application using Ruby.
- Client application using C# in Silverlight.
Creating the server application:
Making a multi-threaded server application in ruby is really easy and that is the reason I chose Ruby to create the server in least amount of time possible.
1: require 'socket'
3: HOST = 'localhost'
4: PORT = 4505
6: server = TCPServer.new(HOST, PORT)
8: # array to store all the active connections
9: sessions = 
10: while (session = server.accept)
11: # push the current session(socket) in the array
12: sessions << session
13: # initialize a new thead for each connection
14: Thread.new(session) do |local_session|
15: # each time a client sends some data send it to all the connections
17: data = local_session.gets
18: sessions.each do |s|
20: s.puts data
21: rescue Errno::ECONNRESET
22: # an exception is raised, that means the connection to the client is broken
Here is the pastie.
Save this file as chat_server.rb
Read Full Post
| Make a Comment ( 8 so far )