1.0.61 โ€ข Published 1 year ago

@ditchoom/socket-kt v1.0.61

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

Contributors Forks Stargazers Issues MIT License LinkedIn

About The Project

Managing metwork calls can be slightly different based on each platform. This project aims to make it easier to manage sockets in a cross platform way using kotlin multiplatform. This was originally created as a side project for a kotlin multiplatform mqtt data sync solution.

Runtime Dependencies

Supported Platforms

Platform๐Ÿ› Builds๐Ÿ›  + ๐Ÿ”ฌTests๐Ÿ”ฌDeployed ArtifactNon Kotlin Sample
JVM 1.8๐Ÿš€maven centralWIP
Node.js๐Ÿš€npm ๐Ÿ”ฎWIP
Browser (Chrome)๐Ÿš€unavailableWIP
Android๐Ÿš€maven centralWIP
iOS๐Ÿ”ฎNeed helpWIP
WatchOS๐Ÿ”ฎNeed helpWIP
TvOS๐Ÿ”ฎNeed helpWIP
MacOS๐Ÿ”ฎNeed helpWIP
Linux X64๐Ÿ”ฎNeed helpWIP
Windows X64๐Ÿ”ฎNeed helpWIP

Help needed for getting a native socket to work for other platforms. For Apple products we want to use APIs that work on cellular networks not just WiFi

Installation

  • Add implementation("com.ditchoom:socket:$version") to your build.gradle dependencies

Client Socket Usage

Suspend connect read write and close

// Run in a coroutine scope
val socket = ClientSocket.connect(
    port = 80, // no default
    hostname = "example.com", // null is default which points to localhost
    timeout = 1.seconds, // default
    socketOptions = null, // default
)
val isOpen = socket.isOpen()
val localPort = socket.localPort()
val remotePort = socket.remotePort()
val stringRead = socket.readUtf8() // read a utf8 string
val readBuffer = socket.read() // read a ReadBuffer as defined in the buffer module
val bytesWritten = socket.write(buffer) // write the buffer to the socket
socket.close() // close the socket

Or use lambda which auto closes the socket

// Run in a coroutine scope, same defaults as the other `connect` method
val response = ClientSocket.connect(80, hostname = "example.com") { socket ->
val request =
        """
GET / HTTP/1.1
Host: example.com
Connection: close

"""
    val bytesWritten = socket.write(request)
    socket.read() // can throw a SocketClosedException
}
// response is populated, no need to call socket.close()

TLS support

// Simply add tls=true to your ClientSocket.connect or ClientSocket.allocate
val response = ClientSocket.connect(port, hostname, tls = true) { socket ->
    // do something
}

Building Locally

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the Apache 2.0 License. See LICENSE for more information.

1.0.61

1 year ago

1.0.60

2 years ago

1.0.59

2 years ago

1.0.57

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.8

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.3

2 years ago