swiftでローカルのサーバにアクセスしようとしているのですが、コネクションが確立されていないようで困っています。client.jsからapp.jsへの接続はできているようで、テキストデータは無事に送信されています。
実現したいことは、iphoneアプリのボタンを押して、websocket通信でデータをやり取りすることです。
以下ソースです。
[app.js]
javascript
1var io = require('socket.io').listen(8080); 2 3io.sockets.on('connection', function(socket) { 4 socket.on('don', function(data) { 5 console.log(data); 6 }); 7});
[client.js]
javascript
1var io = require('socket.io-client'); 2var socket = io('http://localhost:8080'); 3 4socket.emit('don', 'doko');
[SubmitViewController.swift]
swift
1// SubmitViewController.swift 2 3import UIKit 4import SocketIO 5 6class SubmitViewController: UIViewController { 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 } 11 12 override func didReceiveMemoryWarning() { 13 super.didReceiveMemoryWarning() 14 } 15 16 @IBAction func submitOffer(_ sender: Any) { 17 let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true)]) 18 let socket = manager.defaultSocket 19 20 socket.on(clientEvent: .connect) {data, ack in 21 print("socket connected") 22 } 23 24 socket.connect() 25 } 26} 27
↓swiftのログ
2017-12-24 17:38:40.486350+0900 mb[3136:72278] [MC] Lazy loading NSBundle MobileCoreServices.framework 2017-12-24 17:38:40.487333+0900 mb[3136:72278] [MC] Loaded MobileCoreServices.framework 2017-12-24 17:38:41.981979+0900 mb[3136:72278] LOG SocketIOClient{/}: Adding handler for event: connect 2017-12-24 17:38:41.982531+0900 mb[3136:72278] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting] 2017-12-24 17:38:41.982776+0900 mb[3136:72278] LOG SocketIOClient{/}: Joining namespace / 2017-12-24 17:38:41.982944+0900 mb[3136:72278] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting 2017-12-24 17:38:41.983064+0900 mb[3136:72278] LOG SocketManager: Adding engine 2017-12-24 17:38:41.983803+0900 mb[3136:72278] LOG SocketManager: Manager is being released 2017-12-24 17:38:41.983840+0900 mb[3136:72378] LOG SocketEngine: Starting engine. Server: http://localhost:8080 2017-12-24 17:38:41.983991+0900 mb[3136:72378] LOG SocketEngine: Handshaking 2017-12-24 17:38:41.983996+0900 mb[3136:72278] LOG SocketIOClient{/}: Client is being released 2017-12-24 17:38:41.985576+0900 mb[3136:72378] LOG SocketEnginePolling: Doing polling GET http://localhost:8080/socket.io/?transport=polling&b64=1 2017-12-24 17:38:42.024909+0900 mb[3136:72376] LOG SocketEnginePolling: Got polling response 2017-12-24 17:38:42.025101+0900 mb[3136:72376] LOG SocketEnginePolling: Got poll message: 97:0{"sid":"o-pOVt74RsYzCWBgAAAD","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40 2017-12-24 17:38:42.025551+0900 mb[3136:72376] LOG SocketEngine: Got message: 0{"sid":"o-pOVt74RsYzCWBgAAAD","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000} 2017-12-24 17:38:42.028485+0900 mb[3136:72376] LOG SocketEngine: Got message: 40 2017-12-24 17:38:42.029058+0900 mb[3136:72376] LOG SocketEngine: Writing poll: has data: false 2017-12-24 17:38:42.029261+0900 mb[3136:72376] LOG SocketEnginePolling: Sending poll: as type: 2 2017-12-24 17:38:42.029794+0900 mb[3136:72376] LOG SocketEnginePolling: Created POST string: 1:2 2017-12-24 17:38:42.030282+0900 mb[3136:72376] LOG SocketEnginePolling: POSTing 2017-12-24 17:38:42.030530+0900 mb[3136:72376] LOG SocketEngine: Engine is being released 2017-12-24 17:39:25.880162+0900 mb[3136:72278] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/jupiter/Library/Developer/CoreSimulator/Devices/64B858AC-110D-44E7-B3E5-D985F8C6E409/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2017-12-24 17:39:25.880771+0900 mb[3136:72278] [MC] Reading from private effective user settings.
英語が読めれば、ドキュメントやエラーメッセージで理解できるの思うのですが。
以下ヴァージョンです
node 9.3.0
socket.io 2.4.0
swift 4.3.0
Xcode 9.2(9C40b)
どうぞよろしくお願いいたします。
あなたの回答
tips
プレビュー