|
the server portion is a dektop app
here is the code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<WebSocketSession> m_Sessions = new List<WebSocketSession>();
private List<WebSocketSession> m_SecureSessions = new List<WebSocketSession>();
WebSocketServer WebSocket;
private void button1_Click(object sender, EventArgs e)
{
WebSocket = new WebSocketServer();
WebSocket.Setup(new RootConfig(),
new ServerConfig
{
Name = "SuperWebSocket",
Ip = "Any",
Port = 3202,
Mode = SocketMode.Async
}, SocketServerFactory.Instance);
WebSocket.NewDataReceived += new SuperWebSocket.SessionEventHandler<SuperWebSocket.WebSocketSession, byte[]>(wss_NewDataReceived);
WebSocket.NewMessageReceived += new SuperWebSocket.SessionEventHandler<SuperWebSocket.WebSocketSession, string>(wss_NewMessageReceived);
WebSocket.NewSessionConnected += new SuperWebSocket.SessionEventHandler<SuperWebSocket.WebSocketSession>(wss_NewSessionConnected);
WebSocket.SessionClosed += new SessionEventHandler<WebSocketSession, SuperSocket.SocketBase.CloseReason>(wss_SessionClosed);
WebSocket.Start();
}
void wss_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason e)
{
textBox2.Invoke((Action)(() => textBox2.AppendText("closed")));
}
void wss_NewSessionConnected(SuperWebSocket.WebSocketSession session)
{
textBox2.Invoke((Action)(() => textBox2.AppendText("opened")));
}
void wss_NewMessageReceived(SuperWebSocket.WebSocketSession session, string e)
{
textBox2.Invoke((Action)(() => textBox2.AppendText(e)));
}
void wss_NewDataReceived(SuperWebSocket.WebSocketSession session, byte[] e)
{
textBox2.Invoke((Action)(() => textBox2.AppendText("data recived")));
}
private void button2_Click(object sender, EventArgs e)
{
// ((WebSocketSession)WebSocket.GetAllSessions()[0]).SendResponse("");
foreach (WebSocketSession wss in WebSocket.GetAllSessions())
{
wss.SendResponse("hello from server");
}
}
}
and client is html page
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://localhost:3202");
ws.onerror = function () {
alert("error");
}
ws.onopen = function () {
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
alert("Message is received..." + received_msg);
};
ws.onclose = function () {
// websocket is closed.
alert("Connection is closed...");
};
}
else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
and i'm runnint in vs2010 with .net 4.0 and my os is win7 64 bit
when i click the anchor tag connection is open on both server and client,but the message is not sent
when i close the browser or refresh the page above message is send server
i dont understand why it is doing this
|