Beginner Help!

Dec 2, 2012 at 11:58 PM

Using WebSocket4Net to connect to Alchemey project.  The websocket_Opened never gets fired off.  I see a connection to the server.  If I put everything in the main class, I can send data and see it interact.  Thoughts?  Thanks in advance..

using System;
using WebSocket4Net;



namespace SignalRConsoleApp
{
    public class Program
    {
        WebSocket4Net.WebSocket websocket;
        static void Main(string[] args)
        {
            WebSocket websocket = new WebSocket("ws://localhost:4001/");
            websocket.Opened += new EventHandler(websocket_Opened);
            //websocket.Error += new EventHandler(websocket_Error);
            //websocket.Closed += new EventHandler(websocket_Closed);
            //websocket.MessageReceived += new EventHandler(websocket_MessageReceived);
            websocket.Open();
        }

        private static void websocket_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("Fired Off and Connected");
        }

        }
        }

        
        
     

Coordinator
Dec 3, 2012 at 7:59 AM

Of course, it is due to the object's life circle.

Dec 3, 2012 at 9:33 PM

Have it now working..thanks for pointing me in the right direction! :)

static void Main(string[] args)
        {
            WebSocket websocket = new WebSocket("ws://localhost:4001/");
            websocket.Opened += new EventHandler(websocket_Opened);
            websocket.MessageReceived += new EventHandler(websocket_recieved);

            websocket.Open();

            while (true)
            {
                var line = Console.ReadLine();

                if (line.Equals("EXIT", StringComparison.OrdinalIgnoreCase))
                {
                    websocket.Close();
                    break;
                }
                websocket.Send(line);
            }
        }

        private static void websocket_recieved(object sender, MessageReceivedEventArgs e)
        {
            Console.WriteLine(e.Message);
        }

        static void websocket_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("Connection Succeded");
        }
    }