2013-01-07 2 views
2

.NET 클라이언트가 메시지를 교환하기 위해 연결할 수있는 간단한 서버를 작성하려고합니다. 모든 클라이언트의 모든 메시지는 다른 모든 메시지로 이동합니다. node.js + socket.io는 여기에가는 좋은 방법 인 것처럼 보이지만 문제가 있습니다. socket.io 사이트의 샘플 코드를 사용하고 있으며 브라우저에서 연결하면 콘솔이 모든 연결 및 하트 비트 이벤트를 추적하므로 최소한 환경 설정이 올바르게되어 있다고 생각합니다. .C#에서 node.js 서버에 연결

내 클라이언트 코드 (아래 붙여 넣기)는 Socket 클래스의 example code과 매우 유사하지만 이상하게 작동합니다. "ProcessConnect"핸들러에서 e.LastOperation == "Connect"및 e.SocketError == "Success"이지만 서버 측 연결 이벤트 핸들러가 실행되지 않습니다. 또한 코드가 "Hello World"를 전송할 때 수신 핸들러도 "Hello World"가 다시 시작되면서 처리됩니다. 나는 이것이 서버에서 오지 않는다는 것을 알고있다. 왜냐하면 서버에서이 작업을 수행 할 코드가 없기 때문이다. 아마 소켓 자체 또는 뭔가에 연결했다 생각하지만 서버를 종료하면 연결이 실패합니다.

분명히 .NET 소켓에 대한 기본적인 내용이 없지만 그 내용이 확실하지 않습니다.

클라이언트 :

public class NodeClient 
{ 
    Socket _Socket; 

    public NodeClient() 
    { 
     SocketAsyncEventArgs socketState = new SocketAsyncEventArgs(); 
     socketState.Completed += SocketState_Completed; 
     socketState.RemoteEndPoint = new DnsEndPoint("localhost", 81); 
     _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     _Socket.ConnectAsync(socketState); 
    } 

    private void SocketState_Completed(object sender, SocketAsyncEventArgs e) 
    { 
     if (e.SocketError != SocketError.Success) 
     { 
      throw new SocketException((int)e.SocketError); 
     } 

     switch (e.LastOperation) 
     { 
      case SocketAsyncOperation.Connect: 
       ProcessConnect(e); 
       break; 
      case SocketAsyncOperation.Receive: 
       ProcessReceive(e); 
       break; 
      case SocketAsyncOperation.Send: 
       ProcessSend(e); 
       break; 
      default: 
       throw new Exception("Invalid operation completed."); 
     } 
    } 

    // Called when a ConnectAsync operation completes 
    private void ProcessConnect(SocketAsyncEventArgs e) 
    { 
     byte[] buffer = Encoding.UTF8.GetBytes("Hello World"); 
     e.SetBuffer(buffer, 0, buffer.Length); 
     bool willRaiseEvent = _Socket.SendAsync(e); 
     if (!willRaiseEvent) 
     { 
      ProcessSend(e); 
     } 
    } 

    // Called when a ReceiveAsync operation completes 
    private void ProcessReceive(SocketAsyncEventArgs e) 
    { 
     string message = Encoding.UTF8.GetString(e.Buffer, 0, e.Buffer.Length); 
    } 


    // Called when a SendAsync operation completes 
    private void ProcessSend(SocketAsyncEventArgs e) 
    { 
     bool willRaiseEvent = _Socket.ReceiveAsync(e); 
     if (!willRaiseEvent) 
     { 
      ProcessReceive(e); 
     } 
    } 
} 

서버 :

var io = require('socket.io').listen(81); 

io.sockets.on('connection', function (socket) { 
    console.log('connected yo'); 
}); 

답변

1

당신이 사용하여 자신의 소켓 코드를 처리 할 시도 할 수 있습니다. 매우 도움이 보이는 http://socketio4net.codeplex.com

+0

을 - 은 당신이 닷넷

SocketIO4Net와 통합을 처리하기 위해 SocketIO4Net 같은 라이브러리를 사용하는 것이 좋습니다 것입니다! 문제는 Silverlight 용으로 컴파일해야합니다. 이처럼 사용할 수없는 여러 클래스가 사용 된 것 같습니다. 어쩌면 내가 이식 할 수 있을지 ... –