nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

Cannot send full JSON data with emit #79

Closed JiggyTank closed 8 years ago

JiggyTank commented 8 years ago

Hi, Thank you very much for this wonderful library. I am facing strange problem. With my local server (development server), when iOS code emit data, it is successfully received by this server. When I upload the same server code to remote (production server), the data received by server has missing character from the end.

Data received by development server: {"apiKey": “XXXXX”,”secKey": “XXXXX”,”userDetails": {"userKey": "jiggy", "status": "online","userDP":{"name":"Jiggy","location": "US","nickname": "jiggy","key":"jiggy"}}}

Data received by production server: {"apiKey": “XXXXX”,”secKey": “XXXXX”,”userDetails": {"userKey": "jiggy", "status": "online","userDP":{"name":"Jiggy","location": "US","nickname": "jiggy","key":"jiggy"}} (There is missing '}' at the end. The missing characters are always there).

Please help me to find way to transmit the complex JSON using emit method. Points:

  1. My development server is HTTP and ubuntu 14.01
  2. My production server is HTTPS and CentOS 7.1

I will appreciate your help as I am struggling for last 3 days.

Thanking you in advance, Jiggy

nuclearace commented 8 years ago

How are you emitting it

JiggyTank commented 8 years ago

Hi, Thank you for your prompt response. Please find the code for sending the data:

import UIKit import AVFoundation

class ViewController: UIViewController, UIAlertViewDelegate {

let socket = SocketIOClient(socketURL: "http://192.168.1.79:8999")
//let socket = SocketIOClient(socketURL: "https://XXX.XXX.com:8888")
var name:String?
var resetAck:SocketAckEmitter?

override func viewDidLoad() {
    super.viewDidLoad()
    self.addHandlers()
    self.socket.connect()
}
func addHandlers() {
       self.socket.on("connect") {data, ack in
        print("socket connected")
        self.authUser()
    }

} func authUser(){ let jsonForAuth = "{\"apiKey\": \"XXXXX_ef\",\"secKey\": \"XXXXX_ef\",\"userDetails\": {\"userKey\": \"jiggy\", \"status\": \"online\",\"userDP\":{\"name\":\"Jiggy\",\"location\": \"India\",\"nickname\": \"jiggy\",\"key\":\"jiggy\"}}}" self.socket.emit("authUser",jsonForAuth.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!) } }

Please let me know if you need anything else also.

Thanking you in advance, Jiggy

nuclearace commented 8 years ago

You can just use a dictionary to send JSON. There's no need to use a string.

JiggyTank commented 8 years ago

Hi Erik, I have tried dictionary also but does not help. My production server is not receiving complete data. There is always missing character from the end. Can you please forward the small code snippet to show the ideal way to transmit the data from emit, if possible for you.

Again thanking you in advance,

Jiggy

nuclearace commented 8 years ago

The ideal way would be to use Dictionaries and just emit the dictionary. If it's working on one server but not the other I would look into the server where it doesn't work. Otherwise you can turn on logging for the socket and see what it's sending.

elmateo487 commented 8 years ago

This is an issue. We can't send json strings. Maybe it is because they are escaped?

2016-02-16 03:30:57.153 Jyncs[7320:1845343] LOG SocketIOClient: Emitting: 2["SendMyInfo","{"userId":"12345","userProfilePicUri":"https:\/\/scontent.xx.fbcdn.net\/hprofile-ash2\/v\/t1.0-1\/p160x160\/10524663_10205554057513607_5904828162099238463_n.jpg?oh=5681ab79d416609d75c3b53bf84c09ed&oe=5727BF9F","userName":"Bob Dole","lastTenSongs":[]}"]
2016-02-16 03:30:57.258 Jyncs[7320:1845345] LOG SocketEngine: Writing ws: 2["SendMyInfo","{"userId":"12345","userProfilePicUri":"https:\/\/scontent.xx.fbcdn.net\/hprofile-ash2\/v\/t1.0-1\/p160x160\/10524663_10205554057513607_5904828162099238463_n.jpg?oh=5681ab79d416609d75c3b53bf84c09ed&oe=5727BF9F","userName":"Bob Dole","lastTenSongs":[]}"] has data: 0
2016-02-16 03:30:57.258 Jyncs[7320:1845345] LOG SocketEngine: Sending ws: 2["SendMyInfo","{"userId":"12345","userProfilePicUri":"https:\/\/scontent.xx.fbcdn.net\/hprofile-ash2\/v\/t1.0-1\/p160x160\/10524663_10205554057513607_5904828162099238463_n.jpg?oh=5681ab79d416609d75c3b53bf84c09ed&oe=5727BF9F","userName":"Bob Dole","lastTenSongs":[]}"] as type: 4

Server side:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket) {
  console.log('User Connected: ', socket.id);;

  socket.on('SendMyInfo', function(msg) {
    io.sockets.emit('UpdateInfo', "test");
  });

  socket.on('JoinUserRoom', function(msg) {
    socket.join(msg);
    console.log("Joined Room:", msg, "has the following users in it.",
      Object.keys(io.sockets.adapter.rooms[msg].sockets));
  });

  socket.on('disconnect', function(msg) {
    console.log(socket.id, "has left / ", msg);
  });
});

http.listen(3000, function() {
  console.log('listening on *:3000');
});
nuclearace commented 8 years ago

You shouldn't be needing to send it as a string, you can just send an NSDictionary, the client will take care of the rest

elmateo487 commented 8 years ago

Thanks, I understand that I am able to send the data as a Dictionary. But some people have projects where complex objects are already setup to be serialized and back through JSON, and being able to send a pure JSON string would make the most sense.

There must be a bug somewhere if you can emit "test", but not "{"JSONexamplefromabove"}" as they both should just be passed as strings.

Allowing this would mean people wouldn't have to worry about converting object types into nsdata, or manually parsing the object into a dictionary if they already have json conversions setup.

nuclearace commented 8 years ago

Okay, I'm surprised I didn't find this sooner. Should be fixed in 5.4.1

elmateo487 commented 8 years ago

Ok, I can confirm, the fix in 5.4.1 fixes this problem. Thank you immensely.