sending meassagge to and from the server fails

Jun 20, 2012 at 2:48 PM

 

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

Coordinator
Jun 20, 2012 at 2:51 PM
Which version SuperWebSocket are you using?
From: [email removed]
Sent: Wednesday, June 20, 2012 10:48 PM
To: [email removed]
Subject: sending meassagge to and from the server fails [SuperWebSocket:360317]

From: haiderali

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>


</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

Jun 20, 2012 at 5:50 PM
Edited Jun 20, 2012 at 6:18 PM

that was a good question , and it answered my problem, what i was doing, i copied the files from svn repositry and it was not working , i downloaded the binary from site and it works now, sorry i should have tried it,(after running first time now same probe i.e cant send messages) another question any plan of wcf with websocket, if it already support it any idea how to do it.

Coordinator
Jun 21, 2012 at 2:08 AM

Microsoft has implemented WCF over websocket in .NET 4.5? I heard it before.

Jun 21, 2012 at 10:59 AM

after this code ran first time , then stop working (still above problem) , then i tested this code on two different computer(32 bit win7) it works perfectly, are the libarary for 64 bit are different  

Jun 24, 2012 at 11:37 AM
Edited Jun 24, 2012 at 11:46 AM

if i open the fiddler it works when i close it it stops working what is happening?if fiddler is off Only connection is established but no message is sent or received.

Coordinator
Jun 26, 2012 at 1:47 AM

It's wired, could you try it one more computers?

Jun 26, 2012 at 9:30 AM

i tried on one more computer(win 7 32 bit ultimate) and works great.and in my case only connection is opened but no message exchanged but when i close the browser i received all the message send by the client.   And sorry if i am disturbing you.

Coordinator
Jun 26, 2012 at 9:34 AM

Did you install any anti-virus softwae? Probably you can trace network communications by WiredShark?

Jun 26, 2012 at 10:11 AM
Edited Jun 26, 2012 at 10:14 AM

i have disabled the antivirus and firewall, but the problem is same, and i think wireShark does not capture localhost packets, and i should use another approach in notifying clients, and thanks for your time .