using
Photon.SocketServer;
public
class
ChatServer : ApplicationBase
{
protected
override
PeerBase CreatePeer(InitRequest initRequest)
{
}
protected
override
void
Setup()
{
}
protected
override
void
TearDown()
{
}
}
using
Photon.SocketServer;
using
PhotonHostRuntimeInterfaces;
public
class
ChatPeer : PeerBase
{
public
ChatPeer(IRpcProtocol protocol, IPhotonPeer unmanagedPeer)
:
base
(protocol, unmanagedPeer)
{
}
protected
override
void
OnDisconnect()
{
}
protected
override
void
OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
}
}
protected
override
PeerBase CreatePeer(InitRequest initRequest)
{
return
new
ChatPeer(initRequest.Protocol, initRequest.PhotonPeer);
}
<
Applications
Default
=
"ChatServer"
>
<
Application
Name
=
"ChatServer"
BaseDirectory
=
"ChatServer"
Assembly
=
"ChatServer"
Type
=
"ChatServer"
/>
</
Applications
>
using
System;
using
System.Collections.Generic;
using
System.Text;
using
ExitGames.Client.Photon;
public
class
ChatClient : IPhotonPeerListener
{
private
bool
connected;
public
static
void
Main()
{
var
client =
new
ChatClient();
var
peer =
new
PhotonPeer(client,
true
);
// connect
client.connected =
false
;
peer.Connect(
"127.0.0.1:4530"
,
"ChatServer"
);
while
(!client.connected)
{
peer.Service();
}
var
buffer =
new
StringBuilder();
while
(
true
)
{
peer.Service();
// read input
if
(Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
if
(key.Key != ConsoleKey.Enter)
{
// store input
buffer.Append(key.KeyChar);
}
else
{
// send to server
var
parameters =
new
Dictionary<
byte
,
object
> { { 1, buffer.ToString() } };
peer.OpCustom(1, parameters,
true
);
buffer.Length = 0;
}
}
}
}
public
void
DebugReturn(DebugLevel level,
string
message)
{
Console.WriteLine(level +
": "
+ message);
}
public
void
OnEvent(EventData eventData)
{
Console.WriteLine(
"Event: "
+ eventData.Code);
if
(eventData.Code == 1)
{
Console.WriteLine(
"Chat: "
+ eventData.Parameters[1]);
}
}
public
void
OnOperationResponse(OperationResponse operationResponse)
{
Console.WriteLine(
"Response: "
+ operationResponse.OperationCode);
}
public
void
OnStatusChanged(StatusCode statusCode)
{
if
(statusCode == StatusCode.Connect)
{
this
.connected =
true
;
}
else
{
Console.WriteLine(
"Status: "
+ statusCode);
}
}
}
protected
override
void
OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
var
response =
new
OperationResponse(operationRequest.OperationCode);
this
.SendOperationResponse(response, sendParameters);
}
using
System;
using
Photon.SocketServer;
using
PhotonHostRuntimeInterfaces;
public
class
ChatPeer : PeerBase
{
private
static
readonly
object
syncRoot =
new
object
();
public
ChatPeer(IRpcProtocol protocol, IPhotonPeer unmanagedPeer)
:
base
(protocol, unmanagedPeer)
{
lock
(syncRoot)
{
BroadcastMessage +=
this
.OnBroadcastMessage;
}
}
private
static
event
Action<ChatPeer, EventData, SendParameters> BroadcastMessage;
protected
override
void
OnDisconnect()
{
lock
(syncRoot)
{
BroadcastMessage -=
this
.OnBroadcastMessage;
}
}
protected
override
void
OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
var
@
event
=
new
EventData(1) { Parameters = operationRequest.Parameters };
lock
(syncRoot)
{
BroadcastMessage(
this
, @
event
, sendParameters);
}
var
response =
new
OperationResponse(operationRequest.OperationCode);
this
.SendOperationResponse(response, sendParameters);
}
private
void
OnBroadcastMessage(ChatPeer peer, EventData @
event
, SendParameters sendParameters)
{
if
(peer !=
this
)
{
this
.SendEvent(@
event
, sendParameters);
}
}
}