Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i am building a server client application using WebsocketServer(0.6)
my problem is when i run this code on my win7 64 bit laptop the in client and server connection open event is called and no messages are exchanged, but when i run same code on win7 32bit code runs perfectly , my question is is i'm doing something wrong or i have to use some other library for 64bit os?next question is can i host it on a websiteor WCF somehow ?

the server portion is a dektop app

here is the code
C#
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

HTML
<!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="java<!-- no -->script:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
Posted
Updated 21-Jun-12 9:06am
v2

u have to use supersocket dll of version 4.0
 
Share this answer
 
In the client code, the ws variable should be global. Now it's local to that function and gets deleted after the function is executed.
 
Share this answer
 
Comments
agha_ali22 31-Jul-12 8:09am    
<!DOCTYPE HTML>
<html>
<head>
<title>content</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js">

</script>
<script type="text/javascript">

var ws;

$(function () {

$('#btn_Start').click(function () {
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 Coonected to server");
alert("Connection opened...");
};
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...");
};
});

$('#btn_Send').click(function () {
ws.send("Custom message");
});
});


</script>
</head>
<body>
<div id="sse">
<!--Run WebSocket-->
<button id="btn_Start">
Start</button>
<button id="btn_Send">
Send</button>
</div>
</body>
</html>

this is not working either ,the messages are not sent but when i close or refresh the browser all the messages are sent to the server at once , the open is called immediately. but when i use the fiddler all the message are send when i press the send button(immediately)
This may work: In VS 2008 goto Project, Properties, Tab Build: Change Platform Target to x86,

Let me know.
 
Share this answer
 
v2
Comments
agha_ali22 21-Jun-12 15:30pm    
i'm using vs 2010 and my Platform Target value to x86, although i changed its value to 64 and to any cpu but does have no affect
John Orendt 21-Jun-12 16:26pm    
If possible, try to run the server on a 32 bit machine, then test the client on both 32 and 64 bit other machines. Let us know the results.
agha_ali22 24-Jun-12 8:11am    
An interesting situation when i run fiddler all works fine , when i close it only connection is opened but no message are exchanged?? what is happening????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900