简体   繁体   中英

SIP TCP/IP Socket with Ruby, Thin and EventMachine

I'm very new to sockets and SIP but I want to create an SIP application, I'm sure I have enough programming experience, but I just need a push forward to get started.

The problem is I don't get response from a socket, and I just don't know how to debug it. I've created an account at 12connect, and got SIP server login information. Now when I send the 'register' message which we need to start with for a SIP connection it just doesn't do anything. This is the code I use to send the message:

require 'tilt'

module Speaker

  HOST = "**.***.**.**"
  PORT = 5060

  SIP_HOST = "vpbx.12connect.com"
  SIP_USER = "username"
  SIP_PASS = "*********"

  class SIPServer

    def initialize

      EM.connect SIP_HOST, PORT, Speaker::SIPClient

    end

  end

  class SIPClient < EventMachine::Connection

    def post_init

      output = Tilt::ERBTemplate.new('tcp/register.erb').render(template_attributes)
      puts "\nSending: \n#{output}"
      send_data output

    end

    def receive_data data

      puts "From EM Socket: \n"+ data.to_s

    end

    def template_attributes
      {
        host: Speaker::HOST,
        port: Speaker::PORT,

        sip_host: Speaker::SIP_HOST,
        sip_user: Speaker::SIP_USER,
        sip_pass: Speaker::SIP_PASS
      }
    end

  end
end

This is the message that's send:

REGISTER sips:vpbx.12connect.com SIP/2.0
CSeq: 1 REGISTER
Via: SIP/2.0/TCP **.**.**.**:5060
From: <sips:username@vpbx.12connect.com>
To: <sips:username@vpbx.12connect.com>
Contact: <sips:username@**.***.**.**:5060>
Allow: INVITE,ACK,OPTIONS,BYE,CANCEL,SUBSCRIBE,NOTIFY,REFER,MESSAGE,INFO,PING
Expires: 3600
Content-Length: 0
Max-Forwards: 70

I run the application locally and forwarded the 5060 port to the Mac running this app.

EDIT:

What I see in the networking layer is 12 TCP packages from and to port 50206 and 5060. The packages go from my computers local IP to my home IP.. Also there is 1 package from port 50205 to 5060, this package goes from my computers local ip to 193.67.129.180 (vpbx.12connect.com). The 12 must be from the test message I send, and the second is the message stated above..

Another remarkable thing is the 12 packages all go by two, one with a bad checksum and a good one, the rest is the same for each two.

Can anyone tell me I'm missing something here, I tried a lot of different things, but as far as I know this is just the way it needs to be done..?

Thanks in advance!

To establish a connection between two machines, you need two addresses and two ports.

From your code:

  • Server: vpbx.12connect.com:5060
  • Client: **.***.**.**:?

The client port is usually chosen by the operating system, so it is probably not 5060.

Either you somehow get the used client port and also communicate this port in the SIP message, or you open a server on your machine, which you then can bind to a specific port (eg 5060).

For SIP I would propose to go with the server approach, since you have to receive messages without connecting to a server before (INVITES).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM